[Redis] RedisUtil工具类

转载:http://blog.csdn.net/liuxiao723846/article/details/50401406

1、使用了jedis客户端,对redis进行了封装,包括:

1)使用了redispool获取连接;以及连接的回收;

2)常用五种数据结构的常用操作封装;

复制代码
复制代码
   1 package redis.utils;
   2 
   3 import java.util.List;
   4 import java.util.Map;
   5 import java.util.Set;
   6 
   7 //import org.apache.log4j.Logger;
   8 
   9 
  10 import redis.clients.jedis.Jedis;
  11 import redis.clients.jedis.JedisPool;
  12 import redis.clients.jedis.JedisPoolConfig; 
  13 import redis.clients.jedis.SortingParams;
  14 import redis.clients.jedis.BinaryClient.LIST_POSITION;
  15 import redis.clients.util.SafeEncoder;
  16 
  17 /**
  18  * @author Mr.hu
  19  * @version crateTime:2013-10-30 下午5:41:30
  20  * Class Explain:JedisUtil  
  21  */
  22 public class JedisUtil { 
  23     
  24      //private Logger log = Logger.getLogger(this.getClass());  
  25      /**缓存生存时间 */
  26      private final int expire = 60000;
  27      /** 操作Key的方法 */
  28      public Keys KEYS;
  29      /** 对存储结构为String类型的操作 */
  30      public Strings STRINGS;
  31      /** 对存储结构为List类型的操作 */
  32      public Lists LISTS;
  33       /** 对存储结构为Set类型的操作 */
  34      public Sets SETS;
  35      /** 对存储结构为HashMap类型的操作 */
  36      public Hash HASH;
  37      /** 对存储结构为Set(排序的)类型的操作 */
  38      public SortSet SORTSET;
  39      private static JedisPool jedisPool = null;  
  40          
  41      private JedisUtil() {   
  42         
  43      } 
  44      static {  
  45             JedisPoolConfig config = new JedisPoolConfig();  
  46             //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;  
  47             //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。  
  48             config.setMaxTotal(500);  
  49             //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。  
  50             config.setMaxIdle(5);  
  51             //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;  
  52             config.setMaxWaitMillis(1000 * 100);  
  53             //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;  
  54             config.setTestOnBorrow(true);  
  55             
  56             //redis如果设置了密码:
  57             /*jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP, 
  58                     JRedisPoolConfig.REDIS_PORT,
  59                     10000,JRedisPoolConfig.REDIS_PASSWORD);    */
  60             
  61             //redis未设置了密码:
  62            jedisPool = new JedisPool(config, "172.30.37.73",6379); 
  63        }
  64      
  65      public JedisPool getPool() {  
  66          return jedisPool; 
  67      }
  68      
  69      /**
  70       * 从jedis连接池中获取获取jedis对象  
  71       * @return
  72       */
  73      public Jedis getJedis() {  
  74          return jedisPool.getResource(); 
  75      }
  76      
  77      
  78      private static final JedisUtil jedisUtil = new JedisUtil();
  79      
  80  
  81     /**
  82      * 获取JedisUtil实例
  83      * @return
  84      */
  85     public static JedisUtil getInstance() {
  86         return jedisUtil; 
  87     }
  88 
  89     /**
  90      * 回收jedis(放到finally中)
  91      * @param jedis
  92      */
  93     public void returnJedis(Jedis jedis) {
  94         if (null != jedis && null != jedisPool) {
  95             jedisPool.returnResource(jedis);
  96         }
  97     } 
  98     
  99     /**
 100      * 销毁连接(放到catch中)
 101      * @param pool
 102      * @param jedis
 103      */
 104     public static void returnBrokenResource(Jedis jedis) {
 105         if (null != jedis && null != jedisPool) {
 106             jedisPool.returnResource(jedis);
 107         }
 108     }
 109 
 110     
 111     /**
 112      * 设置过期时间
 113      * @author ruan 2013-4-11
 114      * @param key
 115      * @param seconds
 116      */
 117     public void expire(String key, int seconds) {
 118         if (seconds <= 0) { 
 119             return;
 120         }
 121         Jedis jedis = getJedis();
 122         jedis.expire(key, seconds);
 123         returnJedis(jedis);
 124     }
 125 
 126     /**
 127      * 设置默认过期时间
 128      * @author ruan 2013-4-11
 129      * @param key
 130      */
 131     public void expire(String key) {
 132         expire(key, expire);
 133     }
 134     
 135     
 136     //*******************************************Keys*******************************************//
 137     public class Keys {
 138 
 139         /**
 140          * 清空所有key
 141          */
 142         public String flushAll() {
 143             Jedis jedis = getJedis();
 144             String stata = jedis.flushAll();
 145             returnJedis(jedis);
 146             return stata;
 147         }
 148 
 149         /**
 150          * 更改key
 151          * @param String oldkey
 152          * @param String  newkey
 153          * @return 状态码
 154          * */
 155         public String rename(String oldkey, String newkey) { 
 156             return rename(SafeEncoder.encode(oldkey),
 157                     SafeEncoder.encode(newkey));
 158         }
 159 
 160         /**
 161          * 更改key,仅当新key不存在时才执行
 162          * @param String oldkey
 163          * @param String newkey 
 164          * @return 状态码
 165          * */
 166         public long renamenx(String oldkey, String newkey) {
 167             Jedis jedis = getJedis();
 168             long status = jedis.renamenx(oldkey, newkey);
 169             returnJedis(jedis);
 170             return status;
 171         }
 172 
 173         /**
 174          * 更改key
 175          * @param String oldkey
 176          * @param String newkey
 177          * @return 状态码
 178          * */
 179         public String rename(byte[] oldkey, byte[] newkey) {
 180             Jedis jedis = getJedis();
 181             String status = jedis.rename(oldkey, newkey);
 182             returnJedis(jedis);
 183             return status;
 184         }
 185 
 186         /**
 187          * 设置key的过期时间,以秒为单位
 188          * @param String key
 189          * @param 时间,已秒为单位
 190          * @return 影响的记录数
 191          * */
 192         public long expired(String key, int seconds) {
 193             Jedis jedis = getJedis();
 194             long count = jedis.expire(key, seconds);
 195             returnJedis(jedis);
 196             return count;
 197         }
 198 
 199         /**
 200          * 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。
 201          * @param String key
 202          * @param 时间,已秒为单位
 203          * @return 影响的记录数
 204          * */
 205         public long expireAt(String key, long timestamp) {
 206             Jedis jedis = getJedis();
 207             long count = jedis.expireAt(key, timestamp);
 208             returnJedis(jedis);
 209             return count;
 210         }
 211 
 212         /**
 213          * 查询key的过期时间
 214          * @param String key
 215          * @return 以秒为单位的时间表示
 216          * */
 217         public long ttl(String key) {
 218             //ShardedJedis sjedis = getShardedJedis();
 219             Jedis sjedis=getJedis(); 
 220             long len = sjedis.ttl(key);
 221             returnJedis(sjedis);
 222             return len;
 223         }
 224 
 225         /**
 226          * 取消对key过期时间的设置
 227          * @param key
 228          * @return 影响的记录数
 229          * */
 230         public long persist(String key) {
 231             Jedis jedis = getJedis();
 232             long count = jedis.persist(key);
 233             returnJedis(jedis);
 234             return count;
 235         }
 236 
 237         /**
 238          * 删除keys对应的记录,可以是多个key
 239          * @param String  ... keys
 240          * @return 删除的记录数
 241          * */
 242         public long del(String... keys) {
 243             Jedis jedis = getJedis();
 244             long count = jedis.del(keys);
 245             returnJedis(jedis);
 246             return count;
 247         }
 248 
 249         /**
 250          * 删除keys对应的记录,可以是多个key
 251          * @param String .. keys
 252          * @return 删除的记录数
 253          * */
 254         public long del(byte[]... keys) {
 255             Jedis jedis = getJedis();
 256             long count = jedis.del(keys);
 257             returnJedis(jedis);
 258             return count;
 259         }
 260 
 261         /**
 262          * 判断key是否存在
 263          * @param String key
 264          * @return boolean
 265          * */
 266         public boolean exists(String key) {
 267             //ShardedJedis sjedis = getShardedJedis();
 268             Jedis sjedis=getJedis();  
 269             boolean exis = sjedis.exists(key);
 270             returnJedis(sjedis);
 271             return exis;
 272         }
 273 
 274         /**
 275          * 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法
 276          * @param String key
 277          * @return List<String> 集合的全部记录
 278          * **/
 279         public List<String> sort(String key) {
 280             //ShardedJedis sjedis = getShardedJedis();
 281             Jedis sjedis=getJedis();  
 282             List<String> list = sjedis.sort(key);
 283             returnJedis(sjedis);
 284             return list;
 285         }
 286 
 287         /**
 288          * 对List,Set,SortSet进行排序或limit
 289          * @param String key
 290          * @param SortingParams parame 定义排序类型或limit的起止位置.
 291          * @return List<String> 全部或部分记录
 292          * **/
 293         public List<String> sort(String key, SortingParams parame) {
 294             //ShardedJedis sjedis = getShardedJedis(); 
 295             Jedis sjedis=getJedis(); 
 296             List<String> list = sjedis.sort(key, parame);
 297             returnJedis(sjedis);
 298             return list;
 299         }
 300 
 301         /**
 302          * 返回指定key存储的类型
 303          * @param String key
 304          * @return String string|list|set|zset|hash
 305          * **/
 306         public String type(String key) {
 307             //ShardedJedis sjedis = getShardedJedis(); 
 308             Jedis sjedis=getJedis();  
 309             String type = sjedis.type(key); 
 310             returnJedis(sjedis);
 311             return type;
 312         }
 313 
 314         /**
 315          * 查找所有匹配给定的模式的键
 316          * @param String  key的表达式,*表示多个,?表示一个
 317          * */
 318         public Set<String> keys(String pattern) {
 319             Jedis jedis = getJedis();
 320             Set<String> set = jedis.keys(pattern);
 321             returnJedis(jedis);
 322             return set;
 323         }
 324     }
 325 
 326     //*******************************************Sets*******************************************//
 327     public class Sets {
 328 
 329         /**
 330          * 向Set添加一条记录,如果member已存在返回0,否则返回1
 331          * @param String  key
 332          * @param String member
 333          * @return 操作码,0或1
 334          * */
 335         public long sadd(String key, String member) {
 336             Jedis jedis = getJedis();
 337             long s = jedis.sadd(key, member);
 338             returnJedis(jedis);
 339             return s;
 340         }
 341 
 342         public long sadd(byte[] key, byte[] member) {
 343             Jedis jedis = getJedis();
 344             long s = jedis.sadd(key, member);
 345             returnJedis(jedis);
 346             return s;
 347         }
 348 
 349         /**
 350          * 获取给定key中元素个数
 351          * @param String key
 352          * @return 元素个数
 353          * */
 354         public long scard(String key) {
 355             //ShardedJedis sjedis = getShardedJedis();
 356             Jedis sjedis = getJedis(); 
 357             long len = sjedis.scard(key);
 358             returnJedis(sjedis);
 359             return len;
 360         }
 361 
 362         /**
 363          * 返回从第一组和所有的给定集合之间的差异的成员
 364          * @param String ... keys
 365          * @return 差异的成员集合
 366          * */
 367         public Set<String> sdiff(String... keys) {
 368             Jedis jedis = getJedis();
 369             Set<String> set = jedis.sdiff(keys);
 370             returnJedis(jedis);
 371             return set;
 372         }
 373 
 374         /**
 375          * 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
 376          * @param String newkey 新结果集的key
 377          * @param String ... keys 比较的集合
 378          * @return 新集合中的记录数
 379          * **/
 380         public long sdiffstore(String newkey, String... keys) {
 381             Jedis jedis = getJedis();
 382             long s = jedis.sdiffstore(newkey, keys);
 383             returnJedis(jedis);
 384             return s;
 385         }
 386 
 387         /**
 388          * 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set
 389          * @param String ... keys
 390          * @return 交集成员的集合
 391          * **/
 392         public Set<String> sinter(String... keys) {
 393             Jedis jedis = getJedis();
 394             Set<String> set = jedis.sinter(keys);
 395             returnJedis(jedis);
 396             return set;
 397         }
 398 
 399         /**
 400          * 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
 401          * @param String  newkey 新结果集的key
 402          * @param String ... keys 比较的集合
 403          * @return 新集合中的记录数
 404          * **/
 405         public long sinterstore(String newkey, String... keys) {
 406             Jedis jedis = getJedis();
 407             long s = jedis.sinterstore(newkey, keys);
 408             returnJedis(jedis);
 409             return s;
 410         }
 411 
 412         /**
 413          * 确定一个给定的值是否存在
 414          * @param String  key
 415          * @param String member 要判断的值
 416          * @return 存在返回1,不存在返回0
 417          * **/
 418         public boolean sismember(String key, String member) {
 419             //ShardedJedis sjedis = getShardedJedis();
 420             Jedis sjedis = getJedis(); 
 421             boolean s = sjedis.sismember(key, member);
 422             returnJedis(sjedis);
 423             return s;
 424         }
 425 
 426         /**
 427          * 返回集合中的所有成员
 428          * @param String  key
 429          * @return 成员集合
 430          * */
 431         public Set<String> smembers(String key) {
 432             //ShardedJedis sjedis = getShardedJedis();
 433             Jedis sjedis = getJedis(); 
 434             Set<String> set = sjedis.smembers(key);
 435             returnJedis(sjedis);
 436             return set;
 437         }
 438 
 439         public Set<byte[]> smembers(byte[] key) {
 440             //ShardedJedis sjedis = getShardedJedis();
 441             Jedis sjedis = getJedis();  
 442             Set<byte[]> set = sjedis.smembers(key);
 443             returnJedis(sjedis);
 444             return set;
 445         }
 446 
 447         /**
 448          * 将成员从源集合移出放入目标集合 <br/>
 449          * 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/>
 450          * 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
 451          * @param String  srckey 源集合
 452          * @param String dstkey 目标集合
 453          * @param String member 源集合中的成员
 454          * @return 状态码,1成功,0失败
 455          * */
 456         public long smove(String srckey, String dstkey, String member) {
 457             Jedis jedis = getJedis();
 458             long s = jedis.smove(srckey, dstkey, member);
 459             returnJedis(jedis);
 460             return s;
 461         }
 462 
 463         /**
 464          * 从集合中删除成员
 465          * @param String  key
 466          * @return 被删除的成员
 467          * */
 468         public String spop(String key) {
 469             Jedis jedis = getJedis();
 470             String s = jedis.spop(key);
 471             returnJedis(jedis);
 472             return s;
 473         }
 474 
 475         /**
 476          * 从集合中删除指定成员
 477          * @param String key
 478          * @param String  member 要删除的成员
 479          * @return 状态码,成功返回1,成员不存在返回0
 480          * */
 481         public long srem(String key, String member) {
 482             Jedis jedis = getJedis();
 483             long s = jedis.srem(key, member);
 484             returnJedis(jedis);
 485             return s;
 486         }
 487 
 488         /**
 489          * 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/>
 490          * @param String  ... keys
 491          * @return 合并后的结果集合
 492          * @see sunionstore
 493          * */
 494         public Set<String> sunion(String... keys) {
 495             Jedis jedis = getJedis();
 496             Set<String> set = jedis.sunion(keys);
 497             returnJedis(jedis);
 498             return set;
 499         }
 500 
 501         /**
 502          * 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖
 503          * @param String  newkey 新集合的key
 504          * @param String ... keys 要合并的集合
 505          * **/
 506         public long sunionstore(String newkey, String... keys) {
 507             Jedis jedis = getJedis();
 508             long s = jedis.sunionstore(newkey, keys);
 509             returnJedis(jedis);
 510             return s;
 511         }
 512     }
 513 
 514     //*******************************************SortSet*******************************************//
 515     public class SortSet {
 516 
 517         /**
 518          * 向集合中增加一条记录,如果这个值已存在,这个值对应的权重将被置为新的权重
 519          * @param String  key
 520          * @param double score 权重
 521          * @param String  member 要加入的值,
 522          * @return 状态码 1成功,0已存在member的值
 523          * */
 524         public long zadd(String key, double score, String member) {
 525             Jedis jedis = getJedis();
 526             long s = jedis.zadd(key, score, member);
 527             returnJedis(jedis);
 528             return s;
 529         }
 530 
 531         /*public long zadd(String key, Map<Double, String> scoreMembers) {
 532             Jedis jedis = getJedis();
 533             long s = jedis.zadd(key, scoreMembers);
 534             returnJedis(jedis);
 535             return s;
 536         }*/
 537 
 538         /**
 539          * 获取集合中元素的数量
 540          * @param String  key
 541          * @return 如果返回0则集合不存在
 542          * */
 543         public long zcard(String key) {
 544             //ShardedJedis sjedis = getShardedJedis();
 545             Jedis sjedis = getJedis();
 546             long len = sjedis.zcard(key);
 547             returnJedis(sjedis);
 548             return len;
 549         }
 550 
 551         /**
 552          * 获取指定权重区间内集合的数量
 553          * @param String key
 554          * @param double min 最小排序位置
 555          * @param double max 最大排序位置
 556          * */
 557         public long zcount(String key, double min, double max) {
 558             //ShardedJedis sjedis = getShardedJedis();
 559             Jedis sjedis = getJedis();
 560             long len = sjedis.zcount(key, min, max);
 561             returnJedis(sjedis);
 562             return len;
 563         }
 564 
 565         /**
 566          * 获得set的长度
 567          * 
 568          * @param key
 569          * @return
 570          */
 571         public long zlength(String key) {
 572             long len = 0;
 573             Set<String> set = zrange(key, 0, -1);
 574             len = set.size();
 575             return len;
 576         }
 577 
 578         /**
 579          * 权重增加给定值,如果给定的member已存在
 580          * @param String  key
 581          * @param double score 要增的权重
 582          * @param String  member 要插入的值
 583          * @return 增后的权重
 584          * */
 585         public double zincrby(String key, double score, String member) {
 586             Jedis jedis = getJedis();
 587             double s = jedis.zincrby(key, score, member);
 588             returnJedis(jedis);
 589             return s;
 590         }
 591 
 592         /**
 593          * 返回指定位置的集合元素,0为第一个元素,-1为最后一个元素
 594          * @param String key
 595          * @param int start 开始位置(包含)
 596          * @param int end 结束位置(包含)
 597          * @return Set<String>
 598          * */
 599         public Set<String> zrange(String key, int start, int end) {
 600             //ShardedJedis sjedis = getShardedJedis();
 601             Jedis sjedis = getJedis(); 
 602             Set<String> set = sjedis.zrange(key, start, end);
 603             returnJedis(sjedis);
 604             return set;
 605         }
 606 
 607         /**
 608          * 返回指定权重区间的元素集合
 609          * @param String key
 610          * @param double min 上限权重
 611          * @param double max 下限权重
 612          * @return Set<String>
 613          * */
 614         public Set<String> zrangeByScore(String key, double min, double max) {
 615             //ShardedJedis sjedis = getShardedJedis();
 616             Jedis sjedis = getJedis(); 
 617             Set<String> set = sjedis.zrangeByScore(key, min, max);
 618             returnJedis(sjedis);
 619             return set;
 620         }
 621 
 622         /**
 623          * 获取指定值在集合中的位置,集合排序从低到高
 624          * @see zrevrank
 625          * @param String key
 626          * @param String member
 627          * @return long 位置
 628          * */
 629         public long zrank(String key, String member) {
 630             //ShardedJedis sjedis = getShardedJedis();
 631             Jedis sjedis = getJedis(); 
 632             long index = sjedis.zrank(key, member);
 633             returnJedis(sjedis);
 634             return index;
 635         }
 636 
 637         /**
 638          * 获取指定值在集合中的位置,集合排序从高到低
 639          * @see zrank
 640          * @param String key
 641          * @param String member
 642          * @return long 位置
 643          * */
 644         public long zrevrank(String key, String member) {
 645             //ShardedJedis sjedis = getShardedJedis();
 646             Jedis sjedis = getJedis(); 
 647             long index = sjedis.zrevrank(key, member);
 648             returnJedis(sjedis);
 649             return index;
 650         }
 651 
 652         /**
 653          * 从集合中删除成员
 654          * @param String key
 655          * @param String member 
 656          * @return 返回1成功
 657          * */
 658         public long zrem(String key, String member) {
 659             Jedis jedis = getJedis();
 660             long s = jedis.zrem(key, member);
 661             returnJedis(jedis);
 662             return s;
 663         }
 664 
 665         /**
 666          * 删除
 667          * @param key
 668          * @return
 669          */
 670         public long zrem(String key) {
 671             Jedis jedis = getJedis();
 672             long s = jedis.del(key);
 673             returnJedis(jedis);
 674             return s;
 675         }
 676 
 677         /**
 678          * 删除给定位置区间的元素
 679          * @param String  key
 680          * @param int start 开始区间,从0开始(包含)
 681          * @param int end 结束区间,-1为最后一个元素(包含)
 682          * @return 删除的数量
 683          * */
 684         public long zremrangeByRank(String key, int start, int end) {
 685             Jedis jedis = getJedis();
 686             long s = jedis.zremrangeByRank(key, start, end);
 687             returnJedis(jedis);
 688             return s;
 689         }
 690 
 691         /**
 692          * 删除给定权重区间的元素
 693          * @param String key
 694          * @param double min 下限权重(包含)
 695          * @param double max 上限权重(包含)
 696          * @return 删除的数量
 697          * */
 698         public long zremrangeByScore(String key, double min, double max) {
 699             Jedis jedis = getJedis();
 700             long s = jedis.zremrangeByScore(key, min, max);
 701             returnJedis(jedis);
 702             return s;
 703         }
 704 
 705         /**
 706          * 获取给定区间的元素,原始按照权重由高到低排序
 707          * @param String  key
 708          * @param int start
 709          * @param int end
 710          * @return Set<String>
 711          * */
 712         public Set<String> zrevrange(String key, int start, int end) {
 713             //ShardedJedis sjedis = getShardedJedis();
 714             Jedis sjedis = getJedis(); 
 715             Set<String> set = sjedis.zrevrange(key, start, end);
 716             returnJedis(sjedis);
 717             return set;
 718         }
 719 
 720         /**
 721          * 获取给定值在集合中的权重
 722          * @param String  key
 723          * @param memeber
 724          * @return double 权重
 725          * */
 726         public double zscore(String key, String memebr) {
 727             //ShardedJedis sjedis = getShardedJedis();
 728             Jedis sjedis = getJedis(); 
 729             Double score = sjedis.zscore(key, memebr);
 730             returnJedis(sjedis);
 731             if (score != null)
 732                 return score;
 733             return 0;
 734         }
 735     }
 736     
 737     //*******************************************Hash*******************************************//
 738     public class Hash {
 739 
 740         /**
 741          * 从hash中删除指定的存储
 742          * @param String key
 743          * @param String  fieid 存储的名字
 744          * @return 状态码,1成功,0失败
 745          * */
 746         public long hdel(String key, String fieid) {
 747             Jedis jedis = getJedis();
 748             long s = jedis.hdel(key, fieid);
 749             returnJedis(jedis);
 750             return s;
 751         }
 752 
 753         public long hdel(String key) {
 754             Jedis jedis = getJedis();
 755             long s = jedis.del(key);
 756             returnJedis(jedis);
 757             return s;
 758         }
 759 
 760         /**
 761          * 测试hash中指定的存储是否存在
 762          * @param String key
 763          * @param String  fieid 存储的名字
 764          * @return 1存在,0不存在
 765          * */
 766         public boolean hexists(String key, String fieid) {
 767             //ShardedJedis sjedis = getShardedJedis();
 768             Jedis sjedis = getJedis(); 
 769             boolean s = sjedis.hexists(key, fieid);
 770             returnJedis(sjedis);
 771             return s;
 772         }
 773 
 774         /**
 775          * 返回hash中指定存储位置的值
 776          * 
 777          * @param String key
 778          * @param String fieid 存储的名字
 779          * @return 存储对应的值
 780          * */
 781         public String hget(String key, String fieid) {
 782             //ShardedJedis sjedis = getShardedJedis();
 783             Jedis sjedis = getJedis(); 
 784             String s = sjedis.hget(key, fieid);
 785             returnJedis(sjedis);
 786             return s;
 787         }
 788 
 789         public byte[] hget(byte[] key, byte[] fieid) {
 790             //ShardedJedis sjedis = getShardedJedis();
 791             Jedis sjedis = getJedis(); 
 792             byte[] s = sjedis.hget(key, fieid);
 793             returnJedis(sjedis);
 794             return s;
 795         }
 796 
 797         /**
 798          * 以Map的形式返回hash中的存储和值
 799          * @param String    key
 800          * @return Map<Strinig,String>
 801          * */
 802         public Map<String, String> hgetAll(String key) {
 803             //ShardedJedis sjedis = getShardedJedis();
 804             Jedis sjedis = getJedis(); 
 805             Map<String, String> map = sjedis.hgetAll(key);
 806             returnJedis(sjedis);
 807             return map;
 808         }
 809 
 810         /**
 811          * 添加一个对应关系
 812          * @param String  key
 813          * @param String fieid
 814          * @param String value
 815          * @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
 816          * **/
 817         public long hset(String key, String fieid, String value) {
 818             Jedis jedis = getJedis();
 819             long s = jedis.hset(key, fieid, value);
 820             returnJedis(jedis);
 821             return s;
 822         }
 823 
 824         public long hset(String key, String fieid, byte[] value) {
 825             Jedis jedis = getJedis();
 826             long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
 827             returnJedis(jedis);
 828             return s;
 829         }
 830 
 831         /**
 832          * 添加对应关系,只有在fieid不存在时才执行
 833          * @param String key
 834          * @param String fieid
 835          * @param String value
 836          * @return 状态码 1成功,0失败fieid已存
 837          * **/
 838         public long hsetnx(String key, String fieid, String value) {
 839             Jedis jedis = getJedis();
 840             long s = jedis.hsetnx(key, fieid, value);
 841             returnJedis(jedis);
 842             return s;
 843         }
 844 
 845         /**
 846          * 获取hash中value的集合
 847          * 
 848          * @param String
 849          *            key
 850          * @return List<String>
 851          * */
 852         public List<String> hvals(String key) {
 853             //ShardedJedis sjedis = getShardedJedis();
 854             Jedis sjedis = getJedis(); 
 855             List<String> list = sjedis.hvals(key);
 856             returnJedis(sjedis);
 857             return list;
 858         }
 859 
 860         /**
 861          * 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型
 862          * @param String  key
 863          * @param String  fieid 存储位置
 864          * @param String long value 要增加的值,可以是负数
 865          * @return 增加指定数字后,存储位置的值
 866          * */
 867         public long hincrby(String key, String fieid, long value) {
 868             Jedis jedis = getJedis();
 869             long s = jedis.hincrBy(key, fieid, value);
 870             returnJedis(jedis);
 871             return s;
 872         }
 873 
 874         /**
 875          * 返回指定hash中的所有存储名字,类似Map中的keySet方法
 876          * @param String key
 877          * @return Set<String> 存储名称的集合
 878          * */
 879         public Set<String> hkeys(String key) {
 880             //ShardedJedis sjedis = getShardedJedis();
 881             Jedis sjedis = getJedis(); 
 882             Set<String> set = sjedis.hkeys(key);
 883             returnJedis(sjedis);
 884             return set;
 885         }
 886 
 887         /**
 888          * 获取hash中存储的个数,类似Map中size方法
 889          * @param String  key
 890          * @return long 存储的个数
 891          * */
 892         public long hlen(String key) {
 893             //ShardedJedis sjedis = getShardedJedis();
 894             Jedis sjedis = getJedis();  
 895             long len = sjedis.hlen(key);
 896             returnJedis(sjedis);
 897             return len;
 898         }
 899 
 900         /**
 901          * 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
 902          * @param String  key
 903          * @param String ... fieids 存储位置
 904          * @return List<String>
 905          * */
 906         public List<String> hmget(String key, String... fieids) {
 907             //ShardedJedis sjedis = getShardedJedis();
 908             Jedis sjedis = getJedis(); 
 909             List<String> list = sjedis.hmget(key, fieids);
 910             returnJedis(sjedis);
 911             return list;
 912         }
 913 
 914         public List<byte[]> hmget(byte[] key, byte[]... fieids) {
 915             //ShardedJedis sjedis = getShardedJedis();
 916             Jedis sjedis = getJedis();  
 917             List<byte[]> list = sjedis.hmget(key, fieids);
 918             returnJedis(sjedis);
 919             return list;
 920         }
 921 
 922         /**
 923          * 添加对应关系,如果对应关系已存在,则覆盖
 924          * @param Strin   key
 925          * @param Map <String,String> 对应关系
 926          * @return 状态,成功返回OK
 927          * */
 928         public String hmset(String key, Map<String, String> map) {
 929             Jedis jedis = getJedis();
 930             String s = jedis.hmset(key, map);
 931             returnJedis(jedis);
 932             return s;
 933         }
 934 
 935         /**
 936          * 添加对应关系,如果对应关系已存在,则覆盖
 937          * @param Strin key
 938          * @param Map <String,String> 对应关系
 939          * @return 状态,成功返回OK
 940          * */
 941         public String hmset(byte[] key, Map<byte[], byte[]> map) {
 942             Jedis jedis = getJedis();
 943             String s = jedis.hmset(key, map);
 944             returnJedis(jedis);
 945             return s;
 946         }
 947 
 948     }
 949     
 950     
 951     //*******************************************Strings*******************************************//
 952     public class Strings {
 953         /**
 954          * 根据key获取记录
 955          * @param String  key
 956          * @return 值
 957          * */
 958         public String get(String key) {
 959             //ShardedJedis sjedis = getShardedJedis();
 960             Jedis sjedis = getJedis();  
 961             String value = sjedis.get(key);
 962             returnJedis(sjedis);
 963             return value;
 964         }
 965 
 966         /**
 967          * 根据key获取记录
 968          * @param byte[] key
 969          * @return 值
 970          * */
 971         public byte[] get(byte[] key) {
 972             //ShardedJedis sjedis = getShardedJedis();
 973             Jedis sjedis = getJedis();  
 974             byte[] value = sjedis.get(key);
 975             returnJedis(sjedis);
 976             return value;
 977         }
 978 
 979         /**
 980          * 添加有过期时间的记录
 981          * 
 982          * @param String  key
 983          * @param int seconds 过期时间,以秒为单位
 984          * @param String value
 985          * @return String 操作状态
 986          * */
 987         public String setEx(String key, int seconds, String value) {
 988             Jedis jedis = getJedis();
 989             String str = jedis.setex(key, seconds, value);
 990             returnJedis(jedis);
 991             return str;
 992         }
 993 
 994         /**
 995          * 添加有过期时间的记录
 996          * 
 997          * @param String key
 998          * @param int seconds 过期时间,以秒为单位
 999          * @param String  value
1000          * @return String 操作状态
1001          * */
1002         public String setEx(byte[] key, int seconds, byte[] value) {
1003             Jedis jedis = getJedis();
1004             String str = jedis.setex(key, seconds, value);
1005             returnJedis(jedis);
1006             return str;
1007         }
1008 
1009         /**
1010          * 添加一条记录,仅当给定的key不存在时才插入
1011          * @param String key
1012          * @param String value
1013          * @return long 状态码,1插入成功且key不存在,0未插入,key存在
1014          * */
1015         public long setnx(String key, String value) {
1016             Jedis jedis = getJedis();
1017             long str = jedis.setnx(key, value);
1018             returnJedis(jedis);
1019             return str;
1020         }
1021 
1022         /**
1023          * 添加记录,如果记录已存在将覆盖原有的value
1024          * @param String key
1025          * @param String value
1026          * @return 状态码
1027          * */
1028         public String set(String key, String value) {
1029             return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
1030         }
1031 
1032         /**
1033          * 添加记录,如果记录已存在将覆盖原有的value
1034          * @param String  key
1035          * @param String value
1036          * @return 状态码
1037          * */
1038         public String set(String key, byte[] value) {
1039             return set(SafeEncoder.encode(key), value);
1040         }
1041 
1042         /**
1043          * 添加记录,如果记录已存在将覆盖原有的value
1044          * @param byte[] key
1045          * @param byte[] value
1046          * @return 状态码
1047          * */
1048         public String set(byte[] key, byte[] value) {
1049             Jedis jedis = getJedis();
1050             String status = jedis.set(key, value);
1051             returnJedis(jedis);
1052             return status;
1053         }
1054 
1055         /**
1056          * 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/>
1057          * 例:String str1="123456789";<br/>
1058          * 对str1操作后setRange(key,4,0000),str1="123400009";
1059          * @param String  key
1060          * @param long offset
1061          * @param String  value
1062          * @return long value的长度
1063          * */
1064         public long setRange(String key, long offset, String value) {
1065             Jedis jedis = getJedis();
1066             long len = jedis.setrange(key, offset, value);
1067             returnJedis(jedis);
1068             return len;
1069         }
1070 
1071         /**
1072          * 在指定的key中追加value
1073          * @param String  key
1074          * @param String value
1075          * @return long 追加后value的长度
1076          * **/
1077         public long append(String key, String value) {
1078             Jedis jedis = getJedis();
1079             long len = jedis.append(key, value);
1080             returnJedis(jedis);
1081             return len;
1082         }
1083 
1084         /**
1085          * 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用
1086          * @param String key
1087          * @param long number 要减去的值
1088          * @return long 减指定值后的值
1089          * */
1090         public long decrBy(String key, long number) {
1091             Jedis jedis = getJedis();
1092             long len = jedis.decrBy(key, number);
1093             returnJedis(jedis);
1094             return len;
1095         }
1096 
1097         /**
1098          * <b>可以作为获取唯一id的方法</b><br/>
1099          * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用
1100          * @param String  key
1101          * @param long number 要减去的值
1102          * @return long 相加后的值
1103          * */
1104         public long incrBy(String key, long number) {
1105             Jedis jedis = getJedis();
1106             long len = jedis.incrBy(key, number);
1107             returnJedis(jedis);
1108             return len;
1109         }
1110 
1111         /**
1112          * 对指定key对应的value进行截取 
1113          * @param String   key
1114          * @param long startOffset 开始位置(包含)
1115          * @param long endOffset 结束位置(包含)
1116          * @return String 截取的值
1117          * */
1118         public String getrange(String key, long startOffset, long endOffset) {
1119             //ShardedJedis sjedis = getShardedJedis();
1120             Jedis sjedis = getJedis();  
1121             String value = sjedis.getrange(key, startOffset, endOffset);
1122             returnJedis(sjedis); 
1123             return value;
1124         }
1125 
1126         /**
1127          * 获取并设置指定key对应的value<br/>
1128          * 如果key存在返回之前的value,否则返回null
1129          * @param String  key
1130          * @param String value
1131          * @return String 原始value或null
1132          * */
1133         public String getSet(String key, String value) {
1134             Jedis jedis = getJedis();
1135             String str = jedis.getSet(key, value);
1136             returnJedis(jedis);
1137             return str;
1138         }
1139 
1140         /**
1141          * 批量获取记录,如果指定的key不存在返回List的对应位置将是null
1142          * @param String keys
1143          * @return List<String> 值得集合
1144          * */
1145         public List<String> mget(String... keys) {
1146             Jedis jedis = getJedis();
1147             List<String> str = jedis.mget(keys);
1148             returnJedis(jedis);
1149             return str;
1150         }
1151 
1152         /**
1153          * 批量存储记录
1154          * @param String keysvalues 例:keysvalues="key1","value1","key2","value2";
1155          * @return String 状态码 
1156          * */
1157         public String mset(String... keysvalues) {
1158             Jedis jedis = getJedis();
1159             String str = jedis.mset(keysvalues);
1160             returnJedis(jedis);
1161             return str;
1162         }
1163 
1164         /**
1165          * 获取key对应的值的长度
1166          * @param String key
1167          * @return value值得长度
1168          * */
1169         public long strlen(String key) {
1170             Jedis jedis = getJedis();
1171             long len = jedis.strlen(key);
1172             returnJedis(jedis);
1173             return len;
1174         }
1175     }
1176     
1177     
1178     //*******************************************Lists*******************************************//
1179     public class Lists {
1180         /**
1181          * List长度
1182          * @param String key
1183          * @return 长度
1184          * */
1185         public long llen(String key) {
1186             return llen(SafeEncoder.encode(key));
1187         }
1188 
1189         /**
1190          * List长度
1191          * @param byte[] key
1192          * @return 长度
1193          * */
1194         public long llen(byte[] key) {
1195             //ShardedJedis sjedis = getShardedJedis();
1196             Jedis sjedis = getJedis();  
1197             long count = sjedis.llen(key);
1198             returnJedis(sjedis);
1199             return count;
1200         }
1201 
1202         /**
1203          * 覆盖操作,将覆盖List中指定位置的值
1204          * @param byte[] key
1205          * @param int index 位置
1206          * @param byte[] value 值
1207          * @return 状态码
1208          * */
1209         public String lset(byte[] key, int index, byte[] value) {
1210             Jedis jedis = getJedis();
1211             String status = jedis.lset(key, index, value);
1212             returnJedis(jedis);
1213             return status;
1214         }
1215 
1216         /**
1217          * 覆盖操作,将覆盖List中指定位置的值
1218          * @param key
1219          * @param int index 位置
1220          * @param String  value 值
1221          * @return 状态码
1222          * */
1223         public String lset(String key, int index, String value) {
1224             return lset(SafeEncoder.encode(key), index,
1225                     SafeEncoder.encode(value));
1226         }
1227 
1228         /**
1229          * 在value的相对位置插入记录
1230          * @param key
1231          * @param LIST_POSITION   前面插入或后面插入
1232          * @param String pivot 相对位置的内容
1233          * @param String value 插入的内容
1234          * @return 记录总数
1235          * */
1236         public long linsert(String key, LIST_POSITION where, String pivot,
1237                 String value) {
1238             return linsert(SafeEncoder.encode(key), where,
1239                     SafeEncoder.encode(pivot), SafeEncoder.encode(value));
1240         }
1241 
1242         /**
1243          * 在指定位置插入记录
1244          * @param String key
1245          * @param LIST_POSITION 前面插入或后面插入
1246          * @param byte[] pivot 相对位置的内容
1247          * @param byte[] value 插入的内容
1248          * @return 记录总数
1249          * */
1250         public long linsert(byte[] key, LIST_POSITION where, byte[] pivot,
1251                 byte[] value) {
1252             Jedis jedis = getJedis();
1253             long count = jedis.linsert(key, where, pivot, value);
1254             returnJedis(jedis);
1255             return count;
1256         }
1257 
1258         /**
1259          * 获取List中指定位置的值
1260          * @param String  key
1261          * @param int index 位置 
1262          * @return 值
1263          * **/
1264         public String lindex(String key, int index) {
1265             return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
1266         }
1267 
1268         /**
1269          * 获取List中指定位置的值 
1270          * @param byte[] key
1271          * @param int index 位置
1272          * @return 值
1273          * **/
1274         public byte[] lindex(byte[] key, int index) { 
1275             //ShardedJedis sjedis = getShardedJedis();
1276             Jedis sjedis = getJedis();  
1277             byte[] value = sjedis.lindex(key, index);
1278             returnJedis(sjedis);
1279             return value;
1280         }
1281 
1282         /**
1283          * 将List中的第一条记录移出List
1284          * @param String key
1285          * @return 移出的记录 
1286          * */
1287         public String lpop(String key) {
1288             return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
1289         }
1290 
1291         /**
1292          * 将List中的第一条记录移出List
1293          * @param byte[] key
1294          * @return 移出的记录
1295          * */
1296         public byte[] lpop(byte[] key) {
1297             Jedis jedis = getJedis();
1298             byte[] value = jedis.lpop(key);
1299             returnJedis(jedis);
1300             return value;
1301         }
1302 
1303         /**
1304          * 将List中最后第一条记录移出List
1305          * 
1306          * @param byte[] key
1307          * @return 移出的记录
1308          * */
1309         public String rpop(String key) {
1310             Jedis jedis = getJedis();
1311             String value = jedis.rpop(key);
1312             returnJedis(jedis);
1313             return value;
1314         }
1315 
1316         /**
1317          * 向List尾部追加记录
1318          * @param String key
1319          * @param String value
1320          * @return 记录总数
1321          * */
1322         public long lpush(String key, String value) {
1323             return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
1324         }
1325 
1326         /**
1327          * 向List头部追加记录
1328          * @param String  key
1329          * @param String  value
1330          * @return 记录总数
1331          * */
1332         public long rpush(String key, String value) {
1333             Jedis jedis = getJedis();
1334             long count = jedis.rpush(key, value);
1335             returnJedis(jedis);
1336             return count;
1337         }
1338 
1339         /**
1340          * 向List头部追加记录
1341          * @param String key
1342          * @param String value
1343          * @return 记录总数
1344          * */
1345         public long rpush(byte[] key, byte[] value) {
1346             Jedis jedis = getJedis();
1347             long count = jedis.rpush(key, value);
1348             returnJedis(jedis);
1349             return count;
1350         }
1351 
1352         /**
1353          * 向List中追加记录
1354          * @param byte[] key
1355          * @param byte[] value
1356          * @return 记录总数
1357          * */
1358         public long lpush(byte[] key, byte[] value) {
1359             Jedis jedis = getJedis();
1360             long count = jedis.lpush(key, value);
1361             returnJedis(jedis);
1362             return count;
1363         }
1364 
1365         /**
1366          * 获取指定范围的记录,可以做为分页使用
1367          * @param String key
1368          * @param long start
1369          * @param long end
1370          * @return List
1371          * */
1372         public List<String> lrange(String key, long start, long end) {
1373             //ShardedJedis sjedis = getShardedJedis();
1374             Jedis sjedis = getJedis();   
1375             List<String> list = sjedis.lrange(key, start, end);
1376             returnJedis(sjedis);
1377             return list;
1378         }
1379 
1380         /**
1381          * 获取指定范围的记录,可以做为分页使用
1382          * @param byte[] key
1383          * @param int start
1384          * @param int end 如果为负数,则尾部开始计算
1385          * @return List
1386          * */
1387         public List<byte[]> lrange(byte[] key, int start, int end) {
1388             //ShardedJedis sjedis = getShardedJedis();
1389             Jedis sjedis = getJedis();   
1390             List<byte[]> list = sjedis.lrange(key, start, end);
1391             returnJedis(sjedis);
1392             return list;
1393         }
1394 
1395         /**
1396          * 删除List中c条记录,被删除的记录值为value
1397          * @param byte[] key
1398          * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
1399          * @param byte[] value 要匹配的值
1400          * @return 删除后的List中的记录数
1401          * */
1402         public long lrem(byte[] key, int c, byte[] value) {
1403             Jedis jedis = getJedis();
1404             long count = jedis.lrem(key, c, value);
1405             returnJedis(jedis);
1406             return count;
1407         }
1408 
1409         /**
1410          * 删除List中c条记录,被删除的记录值为value
1411          * @param String key
1412          * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
1413          * @param String value 要匹配的值
1414          * @return 删除后的List中的记录数
1415          * */
1416         public long lrem(String key, int c, String value) {
1417             return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
1418         }
1419 
1420         /**
1421          * 算是删除吧,只保留start与end之间的记录
1422          * @param byte[] key
1423          * @param int start 记录的开始位置(0表示第一条记录)
1424          * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
1425          * @return 执行状态码
1426          * */
1427         public String ltrim(byte[] key, int start, int end) {
1428             Jedis jedis = getJedis();
1429             String str = jedis.ltrim(key, start, end);
1430             returnJedis(jedis);
1431             return str;
1432         }
1433 
1434         /** 
1435          * 算是删除吧,只保留start与end之间的记录
1436          * @param String key 
1437          * @param int start 记录的开始位置(0表示第一条记录)
1438          * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
1439          * @return 执行状态码
1440          * */
1441         public String ltrim(String key, int start, int end) {
1442             return ltrim(SafeEncoder.encode(key), start, end);
1443         }
1444     } 
1445     
1446     public static void main(String[] args) {
1447         JedisUtil jedisUtil= JedisUtil.getInstance();  
1448         JedisUtil.Strings strings=jedisUtil.new Strings();
1449         strings.set("nnn", "nnnn"); 
1450         System.out.println("-----"+strings.get("nnn"));   
1451         
1452         Jedis jedis=JedisUtil.getInstance().getJedis(); 
1453         for (int i = 0; i < 10; i++) { 
1454             jedis.set("test", "test"); 
1455             System.out.println(i+"=="+jedis.get("test"));  
1456         
1457         }
1458         JedisUtil.getInstance().returnJedis(jedis);   
1459     }
1460         
1461 }
复制代码
复制代码

补充:

复制代码
复制代码
 1     /**
 2      * @Title:
 3      * getList @Description:(根据key以及类得到redis服务器中集合字符串对应的集合) @param @param
 4      * key @param @param clazz @param @return 设定文件 @return List<T> 返回类型 @throws
 5      */
 6     public <T> List<T> getList(String key, Class<T> clazz) {
 7         String valueStr = this.get(key);
 8         if (valueStr == null || "".equals(valueStr))
 9             return null;
10         if (!isGoodJson(valueStr)) {
11             valueStr = JSON.toJSONString(valueStr);
12         }
13         return JSONArray.parseArray(valueStr, clazz);
14     }
15 
16     /**
17      * 
18      * @Title: getObject @Description:
19      * (根据key以及类得到redis服务器中对象字符串对应的对象) @param @param key @param @param
20      * clazz @param @return 设定文件 @return T 返回类型 @throws
21      */
22     public <T> T getObject(String key, Class<T> clazz) {
23         String valueStr = this.get(key);
24         if (valueStr == null || "".equals(valueStr))
25             return null;
26         if (!isGoodJson(valueStr)) {
27             valueStr = JSON.toJSONString(valueStr);
28         }
29         return (T) JSON.parseObject(valueStr, clazz);
30     }
31 
32     /**
33      * 
34      * @Title: isGoodJson @Description: (判断是否是json格式字符串) @param @param
35      * json @param @return 设定文件 @return boolean 返回类型 @throws
36      */
37     public static boolean isGoodJson(String json) {
38         if (StringUtils.isBlank(json)) {
39             return false;
40         }
41         try {
42             JSON.parse(json);
43             return true;
44         } catch (Exception e) {
45             return false;
46         }
47     }
复制代码
复制代码

 

2、序列化、反序列化:

redis服务器本身支持二进制安全的类型,所以可以把一个java对象序列化后存储到redis中。下面封装了一个序列化、反序列化的工具类:

复制代码
复制代码
 1 package redis.utils;
 2 
 3 import java.io.ByteArrayInputStream;
 4 import java.io.ByteArrayOutputStream;
 5 import java.io.ObjectInputStream;
 6 import java.io.ObjectOutputStream;
 7 
 8 public class SerializeUtil {
 9     /**
10      * 序列化
11      * 
12      * @param object
13      * @return
14      */
15     public static byte[] serialize(Object object) {
16         ObjectOutputStream oos = null;
17         ByteArrayOutputStream baos = null;
18         try {
19             // 序列化
20             baos = new ByteArrayOutputStream();
21             oos = new ObjectOutputStream(baos);
22             oos.writeObject(object);
23             byte[] bytes = baos.toByteArray();
24             return bytes;
25         } catch (Exception e) {
26 
27         }
28         return null;
29     }
30 
31     /**
32      * 反序列化
33      * 
34      * @param bytes
35      * @return
36      */
37     public static Object unserialize(byte[] bytes) {
38         ByteArrayInputStream bais = null;
39         try {
40             // 反序列化
41             bais = new ByteArrayInputStream(bytes);
42             ObjectInputStream ois = new ObjectInputStream(bais);
43             return ois.readObject();
44         } catch (Exception e) {
45 
46         }
47         return null;
48     }
49 }
复制代码
复制代码

3、测试:

1)直接使用RedisUtils实例进行五大数据类型的操作:(这样,使用完后会自动归还到池子中)

1 JedisUtil jedisUtil= JedisUtil.getInstance();  
2         JedisUtil.Strings strings=jedisUtil.new Strings();
3         strings.set("nnn", "nnnn"); 
4         System.out.println("-----"+strings.get("nnn"));   

2)通过RedisUtil实例获取Jedis连接对象;这样就可以用原生的方式使用;最后使用完后需要手动将其归还到池子中:

复制代码
复制代码
1                Jedis jedis=JedisUtil.getInstance().getJedis(); 
2         for (int i = 0; i < 10; i++) { 
3             jedis.set("test", "test"); 
4             System.out.println(i+"=="+jedis.get("test"));  
5         
6         }
7         JedisUtil.getInstance().returnJedis(jedis);          
复制代码
复制代码

3)将java对象存到redis中:

复制代码
复制代码
 1        Person p = new Person();
 2         p.setId(3);
 3         p.setName("测试");
 4         
 5         JedisUtil.Strings strings=jedisUtil.new Strings();
 6         strings.set("object3", SerializeUtil.serialize(p));
 7         
 8         //jedis.set(SafeEncoder.encode("object1"),SerializeUtil.serialize(p));
 9         byte[] personBy = jedis.get(SafeEncoder.encode("object3"));
10         Person p1 = (Person) SerializeUtil.unserialize(personBy);
11         System.out.println(p1.getName());
复制代码
复制代码

猜你喜欢

转载自www.cnblogs.com/wxxujian/p/12728682.html