首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

redis的简略使用和介绍(转载)

2012-07-18 
redis的简单使用和介绍(转载)Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached

redis的简单使用和介绍(转载)

Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP,Java客户端,使用很方便。
Redis使用单线程的IO复用模型,自己封装了一个简单的AeEvent事件处理框架,主要实现了epoll、kqueue和select,对于单纯只 有IO操作来说,单线程可以将速度优势发挥到最大,但是Redis也提供了一些简单的计算功能,比如排序、聚合等,对于这些操作,单线程模型实际会严重影 响整体吞吐量,CPU计算过程中,整个IO调度都是被阻塞住的。

Redis 除了作为存储之外还提供了一些其它方面的功能,比如聚合计算、pubsub、scripting等,对于此类功能需要了解其实现原理,清楚地了解到它的局 限性后,才能正确的使用,比如pubsub功能,这个实际是没有任何持久化支持的,消费方连接闪断或重连之间过来的消息是会全部丢失的,又比如聚合计算和 scripting等功能受Redis单线程模型所限,是不可能达到很高的吞吐量的,需要谨慎使用。

本例子Linux采用的centOs5.4

下面来介绍一下redis的安装

?

view plainprint?
  1. wget http://redis.googlecode.com/files/redis-2.0.4.tar.gz tar zxvf redis-2.0.4.tar.gz
  2. cd redis-2.0.4 make

make完后 redis-2.0.4目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli

安装成功


启动服务

?

./redis-server

也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动

./redis-server redis.conf

redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。

启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了

注意启动的时候,会出现

WARNING overcommit_memory is set to 0!Background save may fail under

low memory condition. To fix this issue add'vm.overcommit_memory = 1' to /etc/sysctl.conf and

[6020] 10 Aug 20:58:21 * The server is nowready to accept connections on port 6379

[6020] 10 Aug 20:58:21 - 0 clientsconnected (0 slaves), 533432 bytes in use

[6020] 10 Aug 20:58:30 - 0 clientsconnected (0 slaves), 533432 bytes in use

由于默认配置是连接到本机的

这时候你要修改配置文件的ip地址连接你服务器啊

还有就是执行:sysctl vm.overcommit_memory=1

然后再启动服务就可以了

关于redis一些资料的学习可以到http://www.cnblogs.com/xhan/archive/2011/02/08/1949867.html去学习 ,很全面

下面介绍一个简单java客户端Jedis,大家可以到https://github.com/xetorthio/jedis这网址下载

这里给大家提供一个简单的对jedis的封装类以供参考

Redis.java

?

view plainprint?
  1. package com.ajun.redis;
  2. import java.util.HashMap; import java.util.HashSet;
  3. import java.util.List; import java.util.Map;
  4. import java.util.Set;
  5. import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;
  6. import redis.clients.jedis.JedisPoolConfig; /**
  7. * * @author ajun
  8. * */
  9. public class Redis { private static JedisPool pool;
  10. private static int DBIndex; private static String host;
  11. private static int port=6379; private static int timeout=60*1000;
  12. static { DBIndex=Integer.parseInt(PubConstant.getConfigureValue("redis_dbindex"));
  13. host=PubConstant.getConfigureValue("redis_host");
  14. JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(100);
  15. config.setMaxIdle(20); config.setMaxWait((long)1000);
  16. config.setTestOnBorrow(false); pool = new JedisPool(config, host, port, timeout);//线程数量限制,IP地址,端口,超时时间
  17. }
  18. public static void addItemToList(String key,byte[] value) {
  19. Jedis jedis=null; try {
  20. jedis = pool.getResource(); jedis.connect();
  21. jedis.select(DBIndex); jedis.lpush(key.getBytes(), value);
  22. } catch (Exception e) {
  23. e.printStackTrace(); }
  24. finally{ if(jedis!=null)
  25. pool.returnResource(jedis); }
  26. } @SuppressWarnings("finally")
  27. public static List<String> getItemFromList(String key) {
  28. Jedis jedis=null; //byte[] s=null;
  29. List<String> ss=null; try {
  30. jedis = pool.getResource(); jedis.select(DBIndex);
  31. long len=jedis.llen(key); if(len==0) return null;
  32. ss = jedis.lrange(key, 0, (int)len); } catch (Exception e) {
  33. e.printStackTrace();
  34. } finally{
  35. if(jedis!=null) pool.returnResource(jedis);
  36. return ss; }
  37. }
  38. public static void addItem(String key,byte[] value) {
  39. Jedis jedis=null; try {
  40. jedis = pool.getResource(); jedis.select(DBIndex);
  41. jedis.set(key.getBytes(), value);
  42. } catch (Exception e) { e.printStackTrace();
  43. } finally{
  44. if(jedis!=null) pool.returnResource(jedis);
  45. }
  46. } public static byte[] getItem(String key)
  47. { Jedis jedis=null;
  48. byte[] s=null; try {
  49. jedis = pool.getResource(); jedis.select(DBIndex);
  50. s = jedis.get(key.getBytes()); return s;
  51. } catch (Exception e) { e.printStackTrace();
  52. return s; }
  53. finally{ if(jedis!=null)
  54. pool.returnResource(jedis);
  55. }
  56. }
  57. public static void delItem(String key)
  58. { Jedis jedis=null;
  59. try { jedis = pool.getResource();
  60. jedis.select(DBIndex); jedis.del(key.getBytes());
  61. } catch (Exception e) {
  62. e.printStackTrace(); }
  63. finally{ if(jedis!=null)
  64. pool.returnResource(jedis); }
  65. }
  66. public static long getIncrement(String key) {
  67. Jedis jedis=null; try {
  68. jedis = pool.getResource(); jedis.select(DBIndex);
  69. return jedis.incr(key);
  70. } catch (Exception e) { e.printStackTrace();
  71. return 0L; }
  72. finally{ if(jedis!=null)
  73. pool.returnResource(jedis); }
  74. }
  75. /**
  76. * 设置map 可以存储用户信息 * @param key
  77. * @param map */
  78. public static void setHashMap(String key,HashMap<String,String> map){ Jedis jedis=null;
  79. try { jedis = pool.getResource();
  80. jedis.select(DBIndex); if(map!=null && !map.isEmpty()){
  81. for(Map.Entry<String, String> entry : map.entrySet()){ jedis.hset(key, entry.getKey(), entry.getValue());
  82. }
  83. } } catch (Exception e) {
  84. e.printStackTrace(); }finally{
  85. if(jedis!=null) pool.returnResource(jedis);
  86. }
  87. }
  88. public static Map<String,String> getHashMap(String key){ Map<String,String> map = new HashMap<String,String>();
  89. Jedis jedis=null; try {
  90. jedis = pool.getResource(); jedis.select(DBIndex);
  91. map = jedis.hgetAll(key); } catch (Exception e) {
  92. e.printStackTrace(); }finally{
  93. if(jedis!=null) pool.returnResource(jedis);
  94. } return map;
  95. }
  96. /**
  97. * 添加set * @param key
  98. * @param set */
  99. public static void addSet(String key,Set<String> set){ Jedis jedis=null;
  100. try { jedis = pool.getResource();
  101. jedis.select(DBIndex); if(set!=null && !set.isEmpty()){
  102. for(String value : set){ /*for ( Iterator<String> memberItr =
  103. jedis.smembers(str).iterator();//返回key对应set的所有元素,结果是无序的 memberItr.hasNext();){
  104. final String member = memberItr.next(); if (!jedis.sismember(str, member)){
  105. jedis.srem(str, member); }
  106. }*/ jedis.sadd(key, value);
  107. } }
  108. } catch (Exception e) { e.printStackTrace();
  109. }finally{ if(jedis!=null)
  110. pool.returnResource(jedis); }
  111. }
  112. public static Set<String> getSet(String key){ Set<String> sets = new HashSet<String>();
  113. Jedis jedis=null; try {
  114. jedis = pool.getResource(); jedis.select(DBIndex);
  115. sets = jedis.smembers(key); } catch (Exception e) {
  116. e.printStackTrace(); }finally{
  117. if(jedis!=null) pool.returnResource(jedis);
  118. }
  119. return sets; }
  120. }

热点排行