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. hdu 1.2.8

    #include<cstdio> #include<iostream> using namespace std; int pow(int a,int b) { ; while( ...

  2. LDAP常用属性及其描述

    属性 全名 描述 dn distinguished name 唯一标识名,类似于绝对路径,每个对象都有唯一标识名. 例如:uid=tester,ou=People,dc=example,dc=com ...

  3. neo4j CQL 使用

    neo4j CQL 使用 1. create命令 CREATE (emp:Employee) #创建一个emp 员工标签 CREATE (dept:Dept) #部门标签 #Added 1 label ...

  4. 键'attachdbfilename'的值无效。

    ---恢复内容开始--- ---恢复内容结束---

  5. windform 重绘Treeview "+-"号图标

    模仿wind系统界面,重绘Treeview + - 号图标 一,首先需要图片 ,用于替换原有的 +-号 二.新建Tree扩展类 TreeViewEx继承TreeView using System; u ...

  6. 拖拽TreeViewItem到OCX控件

    由于C#在性能方面,和C++还是有不少的差距,所以在项目中有一块是用C++的OCX控件实现,然后包括在WPF项目中.由于C++,C#属于不同的体系架构,造成了许多问题,特使是拖拽TreeViewIte ...

  7. centos下配置nginx遇到的一些基本的坑

    作为一个用.net的渣渣,常年混迹在window平台下,对Linux啥都不懂.随着.net core开源.跨平台后,也开始学习下linux. 在Desktop/Webs下放了一个index.html的 ...

  8. oracle数据库迁移相关

    常见的实现方式: rman exp/imp  expdp/impdp DG OGG 主要是看停机时间了,方法很多,数据量小,就导出,如果时间要求很高,那可以采取dg或ogg或类似的技术.减低downt ...

  9. vtk文件编写

    在paraview中加载vtk文件,可以很好的显示三维空间图像,如下cpp代码: #include <iostream> #include <fstream> #include ...

  10. Zookeeper--0200--安装与集群搭建、常用命令、客户端工具

    看这里,http://www.cnblogs.com/lihaoyang/p/8358153.html 1,先使用可视化客户端软件 ZooInspector 连接上集群中的一个节点,看下zk的结构: ...