手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Redis的简单使用和介绍 linux(centos 5.4) redis install

[日期:2012-03-13] 来源:Linux社区  作者:ajun_studio [字体:大 中 小]

关于Redis一些资料的学习可以到http://www.linuxidc.com/search.aspx?Where=Nkey&Keyword=Redis去学习 ,很全面

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

这里给大家提供一个简单的对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. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款