1、序列化工具类

 package com.qicheshetuan.backend.util;

 import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; public class SerializeUtil { //序列化
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try { baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
}
return null;
}
//反序列化
public static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try { bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
}

SerializeUtil

2、redis工具类

 @Component
public class RedisClientUtil {
@Autowired
private JedisPool jedisPool; /**
* 获取Jedis实例
*
* @return
*/
public Jedis getJedis() {
return jedisPool.getResource();
} /**
* 判断某个key是否存在
*
* @param key
* @return
*/
public boolean exist(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 返还到连接池
returnResource(jedis);
}
return false;
} /**
* 以key删除某个数据
*
* @param key
* @return
*/
public Long del(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
// 返还到连接池
returnResource(jedis);
}
}
/**
* 将jedis返还到连接池
*
* @param jedis
*/
public void returnResource(Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
} /**
* 存放数据
*
* @param key 存储的key
* @param value 需要存储的数据
* @param express key失效时间
* @return
*/
public <T> boolean setObject(String key, T value, int express) {
Jedis jedis = null;
try {
jedis = getJedis();
byte[] bytes = SerializeUtil.serialize(value);
jedis.set(key.getBytes(), bytes);
jedis.expire(key, express);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
returnResource(jedis);
}
return false;
} /**
* 删除key集合
*/
public <T> boolean delKeys(List<String> keys) {
Jedis jedis = null;
try {
jedis = getJedis();
for (String key : keys) {
jedis.del(key.getBytes());
}
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
returnResource(jedis);
}
return false;
} /**
* 获取数据
*
* @param key 存储的key
* @return
*/
public <T> T getObject(String key) {
Object value = null;
Jedis jedis = null;
try {
jedis = getJedis();
byte[] bytes = jedis.get(key.getBytes());
value = SerializeUtil.unserialize(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 返还到连接池
returnResource(jedis);
}
if (value != null) {
return (T) value;
}
return null;
} /**
* 将key的时间置为0,即清除缓存
*
* @param key 将key的时间置为0,即清除缓存
*/
public void expire(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.expire(key, 0);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 返还到连接池
returnResource(jedis);
}
} /**
* 删除以某字符串为前缀的key集合
*/
public <T> boolean delKeysMatch(String keyMatch) {
Jedis jedis = null;
try {
jedis = getJedis();
Set<String> keys = jedis.keys(keyMatch + "*");
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String keyStr = it.next();
jedis.del(keyStr);
}
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
//返还到连接池
returnResource(jedis);
}
return false;
}
}

RedisClientUtil

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

  1. redis缓存工具类

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis ...

  2. spring boot 结合Redis 实现工具类

    自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</g ...

  3. Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!

    Go/Python/Erlang编程语言对比分析及示例   本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...

  4. 最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!

    转载自:https://github.com/whvcse/RedisUtil 代码 ProtoStuffSerializerUtil.java import java.io.ByteArrayInp ...

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

    开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java. (1)Java的安装配置可以参考我们的 Java ...

  6. Cache【硬盘缓存工具类(包含内存缓存LruCache和磁盘缓存DiskLruCache)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 内存缓存LruCache和磁盘缓存DiskLruCache的封装类,主要用于图片缓存. 效果图 代码分析 内存缓存LruCache和 ...

  7. 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; usi ...

  8. 封装php redis缓存操作类

    封装php redis缓存操作类,集成了连接redis并判断连接是否成功,redis数据库选择,检测redis键是否存在,获取值,写入值,设置生存时间和删除清空操作. php redis类代码: &l ...

  9. 反射工具类.提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class,被AOP过的真实类等工具函数.java

    import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.ap ...

随机推荐

  1. CentOS6.5分区与文件系统

    1 分区介绍 inux分区不同于windows,linux下硬盘设备名为(IDE硬盘为hdx(x为从a—d)因为IDE硬盘最多四个,SCSI,SATA,USB硬盘为sdx(x为a—z)),硬盘主分区最 ...

  2. Xcode一些好用的插件,以及这些插件的管理器

    最近从xcode6.4升级到xcode7,发现以前所有的插件都失效了,如果要安装,需要重新去一个个下载.安装,很麻烦. 于是,转来了这篇博文,亲自测试,发现很好用...... 地址:http://11 ...

  3. letcode code]Maximum Subarray

    1 题目: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  4. 代码面试集锦 1 - Uber

    Given an array of integers, return a new array such that each element at index i of the new array is ...

  5. 实验6 LCD接口

    1.利用单片机控制LCD1602,在LCD1602上显示字符串,并使其整屏左移. #include<reg51.h> #define uchar unsigned char #define ...

  6. .NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序

    先贴上解决方案截图 一.新建4个解决方案文件夹 1-Presentation 2-Application 3-Domain 4-Infrastructure 二.在解决方案文件夹中分别创建项目 其余项 ...

  7. C# 嵌入dll

    在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转移,相比于一个单独干净的exe,这种形式总归让人不爽,那么有 ...

  8. 未能加载文件或程序集,PublicKeyToken=“**********”,或它的某一个依赖项。强名称验证失败。

    就是这种错误.这种错误怎么办? 以下步骤: (以上图dll为例) 1.看项目的Debug文件夹下是否有以下三个文件 2.看项目的.csproj文件下引用的报错dll的publickeytoken和版本 ...

  9. ionic3.x angular4.x ng4.x 自定义组件component双向绑定之自定义计数器

    本文主要示例在ionic3.x环境下实现一个自定义计数器,实现后最终效果如图: 1.使用命令创建一个component ionic g component CounterInput 类似的命令还有: ...

  10. redis学习笔记-redis的安装

    Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择, ...