本帖最后由 pig2 于 2014-1-4 16:21 编辑
2. tar zxvf redis-2.0.4.tar.gz 3. cd redis-2.0.4 4. make make完后 redis-2.0.4目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli 安装成功
启动服务
./redis-server 也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动 ./redis-serverredis.conf redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。 启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了 注意启动的时候,会出现 WARNINGovercommit_memory is set to 0!Background save may fail under low memorycondition. To fix this issue add'vm.overcommit_memory = 1' to /etc/sysctl.confand [6020] 10 Aug20:58:21 * The server is nowready to accept connections on port 6379 [6020] 10 Aug20:58:21 - 0 clientsconnected (0 slaves), 533432 bytes in use [6020] 10 Aug20:58:30 - 0 clientsconnected (0 slaves), 533432 bytes in use 由于默认配置是连接到本机的 这时候你要修改配置文件的ip地址连接你服务器啊 还有就是执行:sysctlvm.overcommit_memory=1 然后再启动服务就可以了
这里给大家提供一个简单的对jedis的封装类以供参考 Redis.java
1. package com.ajun.redis; 2. 3. import java.util.HashMap; 4. import java.util.HashSet; 5. import java.util.List; 6. import java.util.Map; 7. import java.util.Set; 8. 9. import redis.clients.jedis.Jedis; 10. import redis.clients.jedis.JedisPool; 11. import redis.clients.jedis.JedisPoolConfig; 12. /** 13. * 14. * @author ajun 15. * 16. */ 17. public class Redis { 18. private static JedisPool pool; 19. private static int DBIndex; 20. private static String host; 21. private static int port=6379; 22. private static int timeout=60*1000; 23. static { 24. DBIndex=Integer.parseInt(PubConstant.getConfigureValue("redis_dbindex")); 25. host=PubConstant.getConfigureValue("redis_host"); 26. 27. JedisPoolConfig config = new JedisPoolConfig(); 28. config.setMaxActive(100); 29. config.setMaxIdle(20); 30. config.setMaxWait((long)1000); 31. config.setTestOnBorrow(false); 32. pool = new JedisPool(config, host, port, timeout);//线程数量限制,IP地址,端口,超时时间 33. 34. } 35. public static void addItemToList(String key,byte[] value) 36. { 37. Jedis jedis=null; 38. try { 39. jedis = pool.getResource(); 40. jedis.connect(); 41. jedis.select(DBIndex); 42. jedis.lpush(key.getBytes(), value); 43. 44. } catch (Exception e) { 45. e.printStackTrace(); 46. } 47. finally{ 48. if(jedis!=null) 49. pool.returnResource(jedis); 50. } 51. } 52. @SuppressWarnings("finally") 53. public static List<String> getItemFromList(String key) 54. { 55. Jedis jedis=null; 56. //byte[] s=null; 57. List<String> ss=null; 58. try { 59. jedis = pool.getResource(); 60. jedis.select(DBIndex); 61. long len=jedis.llen(key); 62. if(len==0) return null; 63. ss = jedis.lrange(key, 0, (int)len); 64. } catch (Exception e) { 65. e.printStackTrace(); 66. 67. } 68. finally{ 69. if(jedis!=null) 70. pool.returnResource(jedis); 71. return ss; 72. } 73. 74. } 75. public static void addItem(String key,byte[] value) 76. { 77. Jedis jedis=null; 78. try { 79. jedis = pool.getResource(); 80. jedis.select(DBIndex); 81. jedis.set(key.getBytes(), value); 82. 83. } catch (Exception e) { 84. e.printStackTrace(); 85. } 86. finally{ 87. if(jedis!=null) 88. pool.returnResource(jedis); 89. } 90. 91. } 92. public static byte[] getItem(String key) 93. { 94. Jedis jedis=null; 95. byte[] s=null; 96. try { 97. jedis = pool.getResource(); 98. jedis.select(DBIndex); 99. s = jedis.get(key.getBytes()); 100. return s; 101. } catch (Exception e) { 102. e.printStackTrace(); 103. return s; 104. } 105. finally{ 106. if(jedis!=null) 107. pool.returnResource(jedis); 108. 109. } 110. 111. 112. } 113. 114. public static void delItem(String key) 115. { 116. Jedis jedis=null; 117. try { 118. jedis = pool.getResource(); 119. jedis.select(DBIndex); 120. jedis.del(key.getBytes()); 121. 122. } catch (Exception e) { 123. e.printStackTrace(); 124. } 125. finally{ 126. if(jedis!=null) 127. pool.returnResource(jedis); 128. } 129. 130. } 131. public static long getIncrement(String key) 132. { 133. Jedis jedis=null; 134. try { 135. jedis = pool.getResource(); 136. jedis.select(DBIndex); 137. return jedis.incr(key); 138. 139. } catch (Exception e) { 140. e.printStackTrace(); 141. return 0L; 142. } 143. finally{ 144. if(jedis!=null) 145. pool.returnResource(jedis); 146. } 147. 148. } 149. 150. /** 151. * 设置map 可以存储用户信息 152. * @param key 153. * @param map 154. */ 155. public static void setHashMap(String key,HashMap<String,String> map){ 156. Jedis jedis=null; 157. try { 158. jedis = pool.getResource(); 159. jedis.select(DBIndex); 160. if(map!=null && !map.isEmpty()){ 161. for(Map.Entry<String, String> entry : map.entrySet()){ 162. jedis.hset(key, entry.getKey(), entry.getValue()); 163. } 164. 165. } 166. } catch (Exception e) { 167. e.printStackTrace(); 168. }finally{ 169. if(jedis!=null) 170. pool.returnResource(jedis); 171. } 172. 173. } 174. 175. public static Map<String,String> getHashMap(String key){ 176. Map<String,String> map = new HashMap<String,String>(); 177. Jedis jedis=null; 178. try { 179. jedis = pool.getResource(); 180. jedis.select(DBIndex); 181. map = jedis.hgetAll(key); 182. } catch (Exception e) { 183. e.printStackTrace(); 184. }finally{ 185. if(jedis!=null) 186. pool.returnResource(jedis); 187. } 188. return map; 189. 190. } 191. 192. /** 193. * 添加set 194. * @param key 195. * @param set 196. */ 197. public static void addSet(String key,Set<String> set){ 198. Jedis jedis=null; 199. try { 200. jedis = pool.getResource(); 201. jedis.select(DBIndex); 202. if(set!=null && !set.isEmpty()){ 203. for(String value : set){ 204. /*for ( Iterator<String> memberItr = 205. jedis.smembers(str).iterator();//返回key对应set的所有元素,结果是无序的 206. memberItr.hasNext();){ 207. final String member = memberItr.next(); 208. if (!jedis.sismember(str, member)){ 209. jedis.srem(str, member); 210. } 211. }*/ 212. jedis.sadd(key, value); 213. } 214. } 215. } catch (Exception e) { 216. e.printStackTrace(); 217. }finally{ 218. if(jedis!=null) 219. pool.returnResource(jedis); 220. } 221. } 222. 223. public static Set<String> getSet(String key){ 224. Set<String> sets = new HashSet<String>(); 225. Jedis jedis=null; 226. try { 227. jedis = pool.getResource(); 228. jedis.select(DBIndex); 229. sets = jedis.smembers(key); 230. } catch (Exception e) { 231. e.printStackTrace(); 232. }finally{ 233. if(jedis!=null) 234. pool.returnResource(jedis); 235. } 236. 237. return sets; 238. } 239. 240. 241. }
|