zoukankan      html  css  js  c++  java
  • redis缓存工具类,提供序列化接口

    1、序列化工具类

     1 package com.qicheshetuan.backend.util;
     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     public static byte[] serialize(Object object) {
    12         ObjectOutputStream oos = null;
    13         ByteArrayOutputStream baos = null;
    14         try {
    15 
    16             baos = new ByteArrayOutputStream();
    17             oos = new ObjectOutputStream(baos);
    18             oos.writeObject(object);
    19             byte[] bytes = baos.toByteArray();
    20             return bytes;
    21         } catch (Exception e) {
    22         }
    23         return null;
    24     }
    25     //反序列化
    26     public static Object unserialize(byte[] bytes) {
    27         ByteArrayInputStream bais = null;
    28         try {
    29 
    30             bais = new ByteArrayInputStream(bytes);
    31             ObjectInputStream ois = new ObjectInputStream(bais);
    32             return ois.readObject();
    33         } catch (Exception e) {
    34         }
    35         return null;
    36     }
    37 }
    SerializeUtil

    2、redis工具类

      1 @Component
      2 public class RedisClientUtil {
      3     @Autowired
      4     private JedisPool jedisPool;
      5     
      6     /**
      7      * 获取Jedis实例
      8      *
      9      * @return
     10      */
     11     public Jedis getJedis() {
     12         return jedisPool.getResource();
     13     }
     14     
     15     /**
     16      * 判断某个key是否存在
     17      *
     18      * @param key
     19      * @return
     20      */
     21     public boolean exist(String key) {
     22         Jedis jedis = null;
     23         try {
     24             jedis = getJedis();
     25             return jedis.exists(key);
     26         } catch (Exception e) {
     27             e.printStackTrace();
     28         } finally {
     29             // 返还到连接池
     30             returnResource(jedis);
     31         }
     32         return false;
     33     }
     34 
     35     /**
     36      * 以key删除某个数据
     37      *
     38      * @param key
     39      * @return
     40      */
     41     public Long del(String key) {
     42         Jedis jedis = null;
     43         try {
     44             jedis = getJedis();
     45             return jedis.del(key);
     46         } catch (Exception e) {
     47             e.printStackTrace();
     48             return null;
     49         } finally {
     50             // 返还到连接池
     51             returnResource(jedis);
     52         }
     53     }
     54   /**
     55      * 将jedis返还到连接池
     56      *
     57      * @param jedis
     58      */
     59     public void returnResource(Jedis jedis) {
     60         if (jedis != null) {
     61             jedisPool.returnResource(jedis);
     62         }
     63     }
     64 
     65     /**
     66      * 存放数据
     67      *
     68      * @param key     存储的key
     69      * @param value   需要存储的数据
     70      * @param express key失效时间
     71      * @return
     72      */
     73     public <T> boolean setObject(String key, T value, int express) {
     74         Jedis jedis = null;
     75         try {
     76             jedis = getJedis();
     77             byte[] bytes = SerializeUtil.serialize(value);
     78             jedis.set(key.getBytes(), bytes);
     79             jedis.expire(key, express);
     80             return true;
     81         } catch (Exception e) {
     82             e.printStackTrace();
     83         } finally {
     84             //返还到连接池
     85             returnResource(jedis);
     86         }
     87         return false;
     88     }
     89 
     90     /**
     91      * 删除key集合
     92      */
     93     public <T> boolean delKeys(List<String> keys) {
     94         Jedis jedis = null;
     95         try {
     96             jedis = getJedis();
     97             for (String key : keys) {
     98                 jedis.del(key.getBytes());
     99             }
    100             return true;
    101         } catch (Exception e) {
    102             e.printStackTrace();
    103         } finally {
    104             //返还到连接池
    105             returnResource(jedis);
    106         }
    107         return false;
    108     }
    109 
    110     /**
    111      * 获取数据
    112      *
    113      * @param key 存储的key
    114      * @return
    115      */
    116     public <T> T getObject(String key) {
    117         Object value = null;
    118         Jedis jedis = null;
    119         try {
    120             jedis = getJedis();
    121             byte[] bytes = jedis.get(key.getBytes());
    122             value = SerializeUtil.unserialize(bytes);
    123         } catch (Exception e) {
    124             e.printStackTrace();
    125         } finally {
    126             // 返还到连接池
    127             returnResource(jedis);
    128         }
    129         if (value != null) {
    130             return (T) value;
    131         }
    132         return null;
    133     }
    134 
    135     /**
    136      * 将key的时间置为0,即清除缓存
    137      *
    138      * @param key 将key的时间置为0,即清除缓存
    139      */
    140     public void expire(String key) {
    141         Jedis jedis = null;
    142         try {
    143             jedis = getJedis();
    144             jedis.expire(key, 0);
    145         } catch (Exception e) {
    146             e.printStackTrace();
    147         } finally {
    148             // 返还到连接池
    149             returnResource(jedis);
    150         }
    151     }
    152 
    153     /**
    154      * 删除以某字符串为前缀的key集合
    155      */
    156     public <T> boolean delKeysMatch(String keyMatch) {
    157         Jedis jedis = null;
    158         try {
    159             jedis = getJedis();
    160             Set<String> keys = jedis.keys(keyMatch + "*");
    161             Iterator<String> it = keys.iterator();
    162             while (it.hasNext()) {
    163                 String keyStr = it.next();
    164                 jedis.del(keyStr);
    165             }
    166             return true;
    167         } catch (Exception e) {
    168             e.printStackTrace();
    169         } finally {
    170             //返还到连接池
    171             returnResource(jedis);
    172         }
    173         return false;
    174     }
    175 }
    RedisClientUtil
  • 相关阅读:
    深入研究Servlet线程安全性问题
    Sun公司java语言编码规范
    JAVA的内省机制(introspector)与反射机制(reflection)[转]
    Oracle的悲观锁和乐观锁
    java中120个经典问题
    自定义Java异常
    Java事务处理总结
    Tomcat内存溢出的三种情况及解决办法分析
    .net基础
    C#.Net中的转义
  • 原文地址:https://www.cnblogs.com/wiseroll/p/8278776.html
Copyright © 2011-2022 走看看