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

原文地址:https://www.cnblogs.com/wiseroll/p/8278776.html

时间: 2024-08-04 13:46:41

redis缓存工具类,提供序列化接口的相关文章

spring boot 结合Redis 实现工具类

自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>加入配置 # Redis数据库索引(默认为0)spring.redis.database=0# Redis服务器地址

缓存工具类

安卓开发一般都需要进行数据缓存,常用操作老司机已为你封装完毕,经常有小伙伴问怎么判断缓存是否可用,那我告诉你,你可以用这份工具进行存储和查询,具体可以查看源码,现在为你开车,Demo传送门. 站点 缓存工具类 → AppACache put : 保存String数据到缓存中getAsString : 读取String数据getAsJSONObject : 读取JSONObject数据getAsJSONArray : 读取JSONArray数据getAsBinary : 获取byte数据getAs

redis缓存工具Jedis进行跨jvm加锁(分布式应用)

最近使用redis碰到了多个并发处理同一个缓存的情况.在这种情况下需要进行加锁机制.本来想使用java自带的ReadWriteLock进行设置读写锁,这也是上家公司使用的方法.后来经过商讨,给予排除.原因无他,就是java自带的并不能跨jvm进行加锁,意思就是说A服务器上的write锁无法限制B服务器上的同一个方法,也就是说不适用于分布式部署的环境. 后来经过多方面查看资料.最终决定使用redis自身的方法setnx来进行加锁机制.网上有很多关于setnx来进行加锁的方法.不过大部分都会有一个相

一个redis使用工具类

package com.cheng.common.util.cache; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import javax.annotation.Pos

Windows环境下使用Redis缓存工具的图文详细方法

网上找了两篇关于Redis的博客,记录下! Java 使用Redis缓存工具的图文详细方法 Windows环境下使用Redis缓存工具的图文详细方法

Java 使用Redis缓存工具的图文详细方法

开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java. (1)Java的安装配置可以参考我们的 Java开发环境配置 (2)安装了 redis 服务: 请参考:Windows环境下使用Redis缓存工具的图文详细方法 或是: 首先你需要下载驱动包,下载 jedis.jar,确保下载最新驱动包. 在你的classpath中包含该驱动包. 一.新建一个javaweb项目. 1. 新建一个Jedis的项目.

由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 确的浮点数运算,包括加减乘除和四舍五入

public class Arith { /** * 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 确的浮点数运算,包括加减乘除和四舍五入. */ // 默认除法运算精度 private static final int DEF_DIV_SCALE = 10; // 这个类不能实例化 private Arith() { } /** * 提供精确的加法运算. * * @param v1 * 被加数 * @param v2 * 加数 * @return 两个参数的和 */ p

工具类---提供精确的浮点数运算

import java.math.BigDecimal; /** * 由于Java的简单类型不能够精确的对浮点数进行运算, 这个工具类提供精 确的浮点数运算,包括加减乘除和四舍五入. */ public class Arith { // 默认除法运算精度.除法的时候,默认的精确到小数点后10位 private static final int DEF_DIV_SCALE = 10; // 这个类不能实例化 private Arith() { } /** * 提供精确的加法运算. * * @par

封装php redis缓存操作类

封装php redis缓存操作类,集成了连接redis并判断连接是否成功,redis数据库选择,检测redis键是否存在,获取值,写入值,设置生存时间和删除清空操作. php redis类代码: <?php/*** redisdrive.class.php* php redis 操作类**/class redisdrive{ //键名 public $key; //值 public $value; //默认生存时间 public $expire = 86400; /*60*60*24*/ //连