1、什么是redis
Redis的的是完全开源免费的,遵守BSD协议,是一个高性能的键值数据库。是当前最热门的的的NoSql数据库之一,也被人们称为数据结构服务器。Redis 把整个数据库全加载到内存当中进行操作,通过异步操作定期把数据库数据 flush 到硬盘上保存。因为是纯内存操作,所以 Redis 的性能非常出色,每秒处理 10 万次以上的读写操作,是已知性能最快的 Key-Value 数据库。
2、redis好处
(1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1)
(2) 支持丰富数据类型,支持string,list,set,sorted set,hash
(3) 支持事务,操作都是原子性,所谓的原子性就是对数据的更改要么全部执行,要么全部不执行
(4) 丰富的特性:可用于缓存,消息,按key设置过期时间,过期后将会自动删除
3、redis缺点
(1) Redis不具备自动容错和恢复功能,主机从机的宕机都会导致前端部分读写请求失败,需要等待机器重启或者手动切换前端的IP才能恢复。
(2) 主机宕机,宕机前有部分数据未能及时同步到从机,切换IP后还会引入数据不一致的问题,降低了系统的可用性。
(3) Redis的主从复制采用全量复制,复制过程中主机会fork出一个子进程对内存做一份快照,并将子进程的内存快照保存为文件发送给从机,这一过程需要确保主机有足够多的空余内存。若快照文件较大,对集群的服务能力会产生较大的影响,而且复制过程是在从机新加入集群或者从机和主机网络断开重连时都会进行,也就是网络波动都会造成主机和从机间的一次全量的数据复制,这对实际的系统运营造成了不小的麻烦。
(4) Redis较难支持在线扩容,在集群容量达到上限时在线扩容会变得很复杂。为避免这一问题,运维人员在系统上线时必须确保有足够的空间,这对资源造成了很大的浪费。
4、首先在pom文件中导入redis依赖包
<dependency>
<groupId> redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
4、redis工具类
package com.meiman.meimanwallet.utils;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.BinaryClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.util.SafeEncoder;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* reids 工具类
* author: moupeng
* date: 2019/3/25.
*/
public class RedisUtil {
/**
* 默认redis中的缓存有效期20分钟
*/
private final int expire = 20 * 60;
/**
* 对存储结构为String类型的操作
*/
public Strings STRINGS;
/**
* 对存储结构为List类型的操作
*/
public Lists LISTS;
/**
* 对存储结构为Set类型的操作
*/
public Sets SETS;
/**
* 对存储结构为HashMap类型的操作
*/
public Hash HASH;
/**
* 对存储结构为Set(排序的)类型的操作
*/
public SortSet SORTSET;
private static JedisPool jedisPool = null;
static {
JedisPoolConfig config = new JedisPoolConfig();
//控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxTotal(500);
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(5);
//表示当引入一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(1000 * 100);
//在引入一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
//redis如果设置了密码:
/*jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP,
JRedisPoolConfig.REDIS_PORT,
10000,JRedisPoolConfig.REDIS_PASSWORD);*/
//redis未设置了密码:
//redis的主机IP地址
String redisHost = PropertiesUtils.readProperty("redis.host");
//redis的端口号
Integer redisPort = Integer.parseInt(PropertiesUtils.readProperty("redis.port"));
jedisPool = new JedisPool(config, redisHost, redisPort);
}
private static volatile RedisUtil redisUtil;
/**
* 获取JedisUtil实例
*
* @return
*/
public static RedisUtil getInstance() {
if (redisUtil == null) {
synchronized (RedisUtil.class) {
if (redisUtil == null) {
redisUtil = new RedisUtil();
}
}
}
return redisUtil;
}
/**
* 私有化构造函数
*/
private RedisUtil() {
STRINGS = new Strings();
LISTS = new Lists();
SETS = new Sets();
HASH = new Hash();
SORTSET = new SortSet();
}
/**
* 从jedis连接池中获取获取jedis对象
*
* @return
*/
public Jedis getJedis() {
return jedisPool.getResource();
}
/**
* 回收jedis(放到finally中)
*
* @param jedis
*/
public void returnJedis(Jedis jedis) {
if (null != jedis) {
jedis.close();
}
}
/**
* 设置过期时间
*
* @param key
* @param seconds
* @author ruan 2013-4-11
*/
public void expire(String key, int seconds) {
if (seconds <= 0) {
return;
}
Jedis jedis = getJedis();
jedis.expire(key, seconds);
returnJedis(jedis);
}
/**
* 设置默认过期时间
*
* @param key
* @author ruan 2013-4-11
*/
public void expire(String key) {
expire(key, expire);
}
/***********************************************通用方法*****************************************************/
/**
* 通用的保存对象的方法,并保证在分布式项目中redis的key值唯一性
*
* @param webName 服务的名称
* @param cacheName 缓存的名称
* @param key 缓存的键名
* @param value 缓存的值
* @return
*/
public String set(String webName, String cacheName, String key, Object value) {
return set(webName + "-" + cacheName + "-" + key, value);
}
/**
* 通用的保存对象的方法,并保证在分布式项目中redis的key值唯一性
*
* @param webName 服务的名称
* @param cacheName 缓存的名称
* @param key 缓存的键名
* @param value 缓存的值
* @param time 缓存的有效期,单位:秒
* @return
*/
public String set(String webName, String cacheName, String key, Object value, int time) {
return set(webName + "-" + cacheName + "-" + key, value, time);
}
/**
* 取出缓存中的对象
*
* @param webName 服务的名称
* @param cacheName 缓存的名称
* @param key 缓存的键名
* @return
*/
public Object get(String webName, String cacheName, String key) {
return get(webName + "-" + cacheName + "-" + key);
}
/**
* 将对象以Json字符串的形式保存
*
* @param webName 服务的名称
* @param cacheName 缓存的名称
* @param key 缓存的键名
* @param json 保存的对象的JSON字符串
* @param time 缓存的有效期,单位:秒
* @return
*/
public String setJsonString(String webName, String cacheName, String key, String json, int time) {
Jedis jedis = getJedis();
String str = null;
try {
str = jedis.set(webName + "-" + cacheName + "-" + key, json);
} catch (Exception e) {
e.printStackTrace();
}
if ((time > 0) && ("OK".equals(str))) {
expire(key, time);
}
returnJedis(jedis);
return str;
}
/**
* 将对象以Json字符串的形式保存
*
* @param webName 服务的名称
* @param cacheName 缓存的名称
* @param key 缓存的键名
* @param json 保存的对象的JSON字符串
* @return
*/
public String setJsonString(String webName, String cacheName, String key, String json) {
Jedis jedis = getJedis();
String str = null;
try {
str = jedis.set(webName + "-" + cacheName + "-" + key, json);
} catch (Exception e) {
e.printStackTrace();
}
returnJedis(jedis);
return str;
}
public <T> T getJsonObject(String webName, String cacheName, String key,Class<T> clazz)
{
Jedis jedis = getJedis();
String value = jedis.get(webName + "-" + cacheName + "-" + key);
T object=null;
try
{
object=JSON.parseObject(value,clazz);
}
catch (Exception e)
{
e.printStackTrace();
}
returnJedis(jedis);
return object;
}
/**
* 删除缓存
*
* @param webName 服务的名称
* @param cacheName 缓存的名称
* @param key 缓存的键名
* @return
*/
public boolean delete(String webName, String cacheName, String key) {
return delete(webName + "-" + cacheName + "-" + key);
}
/**
* 通用的保存对象的方法
*
* @param key
* @param value 保存的值,如果是自定义对象注意要实现了序列化接口Serializable接口
*/
private String set(String key, Object value) {
Jedis jedis = getJedis();
String str = null;
try {
str = jedis.set(key.getBytes(), SerializeUtil.serialize(value));
} catch (Exception e) {
e.printStackTrace();
}
returnJedis(jedis);
return str;
}
/**
* 通用的保存对象的方法
*
* @param key
* @param value 保存的值,如果是自定义对象注意要实现了序列化接口Serializable接口
* @time 过期时间 单位秒
*/
private String set(String key, Object value, int time) {
Jedis jedis = getJedis();
String str = null;
try {
str = jedis.set(key.getBytes(), SerializeUtil.serialize(value));
} catch (Exception e) {
e.printStackTrace();
}
if ((time > 0) && ("OK".equals(str))) {
expire(key, time);
}
returnJedis(jedis);
return str;
}
/**
* 通用的获取的方法
*
* @param key
*/
private Object get(String key) {
Jedis jedis = getJedis();
byte[] getBytes = jedis.get(key.getBytes());
Object value = null;
try {
value = SerializeUtil.unserialize(getBytes);
} catch (Exception e) {
e.printStackTrace();
}
returnJedis(jedis);
return value;
}
/**
* 删除key对应的键值对
*
* @param key
* @return
*/
private boolean delete(String key) {
Jedis jedis = getJedis();
boolean isDelete = false;
if (jedis.exists(key)) {
if (jedis.del(key) == 1) {
isDelete = true;
} else {
isDelete = false;
}
} else {
isDelete = false;
}
return isDelete;
}
/*******************************************Sets*******************************************/
public class Sets {
/**
* 向Set添加一条记录,如果member已存在返回0,否则返回1
*
* @param key
* @param member
* @return 操作码, 0或1
*/
public long sAdd(String key, String... member) {
Jedis jedis = getJedis();
long s = jedis.sadd(key, member);
returnJedis(jedis);
return s;
}
public long sadd(byte[] key, byte[]... member) {
Jedis jedis = getJedis();
long s = jedis.sadd(key, member);
returnJedis(jedis);
return s;
}
/**
* 返回集合中的所有成员
*
* @param key
* @return 成员集合
*/
public Set<String> sget(String key) {
Jedis sjedis = getJedis();
Set<String> set = sjedis.smembers(key);
returnJedis(sjedis);
return set;
}
/**
* 返回集合中的所有成员
*
* @param key
* @return 成员集合
*/
public Set<byte[]> sget(byte[] key) {
Jedis sjedis = getJedis();
Set<byte[]> set = sjedis.smembers(key);
returnJedis(sjedis);
return set;
}
/**
* 获取给定key中元素个数
*
* @param key
* @return 元素个数
*/
public long slen(String key) {
Jedis sjedis = getJedis();
long len = sjedis.scard(key);
returnJedis(sjedis);
return len;
}
/**
* 返回从第一组和所有的给定集合之间的差异的成员
*
* @param keys
* @return 差异的成员集合
*/
public Set<String> sdiff(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sdiff(keys);
returnJedis(jedis);
return set;
}
/**
* 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
*
* @param newkey 新结果集的key
* @param keys 比较的集合
* @return 新集合中的记录数
**/
public long sdiffstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sdiffstore(newkey, keys);
returnJedis(jedis);
return s;
}
/**
* 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set
*
* @param keys
* @return 交集成员的集合
**/
public Set<String> sinter(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sinter(keys);
returnJedis(jedis);
return set;
}
/**
* 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
*
* @param newkey 新结果集的key
* @param keys 比较的集合
* @return 新集合中的记录数
**/
public long sinterstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sinterstore(newkey, keys);
returnJedis(jedis);
return s;
}
/**
* 确定一个给定的值是否存在
*
* @param key
* @param member 要判断的值
* @return 存在返回1,不存在返回0
**/
public boolean sismember(String key, String member) {
Jedis sjedis = getJedis();
boolean s = sjedis.sismember(key, member);
returnJedis(sjedis);
return s;
}
/**
* 将成员从源集合移出放入目标集合 <br/>
* 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/>
* 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
*
* @param srckey 源集合
* @param dstkey 目标集合
* @param member 源集合中的成员
* @return 状态码,1成功,0失败
*/
public long smove(String srckey, String dstkey, String member) {
Jedis jedis = getJedis();
long s = jedis.smove(srckey, dstkey, member);
returnJedis(jedis);
return s;
}
/**
* 从集合中删除成员
*
* @param key
* @return 被删除的成员
*/
public String spop(String key) {
Jedis jedis = getJedis();
String s = jedis.spop(key);
returnJedis(jedis);
return s;
}
/**
* 从集合中删除指定成员
*
* @param key
* @param member 要删除的成员
* @return 状态码,成功返回1,成员不存在返回0
*/
public long srem(String key, String member) {
Jedis jedis = getJedis();
long s = jedis.srem(key, member);
returnJedis(jedis);
return s;
}
/**
* 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/>
*
* @param keys
* @return 合并后的结果集合
*/
public Set<String> sunion(String... keys) {
Jedis jedis = getJedis();
Set<String> set = jedis.sunion(keys);
returnJedis(jedis);
return set;
}
/**
* 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖
*
* @param newkey 新集合的key
* @param keys 要合并的集合
**/
public long sunionstore(String newkey, String... keys) {
Jedis jedis = getJedis();
long s = jedis.sunionstore(newkey, keys);
returnJedis(jedis);
return s;
}
}
/*******************************************SortSet*******************************************/
public class SortSet {
/**
* 向集合中增加一条记录,如果这个值已存在,这个值对应的权重将被置为新的权重
*
* @param key
* @param score 权重
* @param member 要加入的值,
* @return 状态码 1成功,0已存在member的值
*/
public long zadd(String key, double score, String member) {
Jedis jedis = getJedis();
long s = jedis.zadd(key, score, member);
returnJedis(jedis);
return s;
}
/**
* 获取集合中所有元素
*
* @param key
* @return 如果返回0则集合不存在
*/
public Set<String> zget(String key) {
Jedis sjedis = getJedis();
Set<String> set = zrange(key, 0, -1);
returnJedis(sjedis);
return set;
}
/**
* 获取集合中元素的数量
*
* @param key
* @return 如果返回0则集合不存在
*/
public long zlen(String key) {
Jedis sjedis = getJedis();
long len = sjedis.zcard(key);
returnJedis(sjedis);
return len;
}
/**
* 获取指定权重区间内集合的数量
*
* @param key
* @param min 最小排序位置
* @param max 最大排序位置
*/
public long zcount(String key, double min, double max) {
Jedis sjedis = getJedis();
long len = sjedis.zcount(key, min, max);
returnJedis(sjedis);
return len;
}
/**
* 权重增加给定值,如果给定的member已存在
*
* @param key
* @param score 要增的权重
* @param member 要插入的值
* @return 增后的权重
*/
public double zincrby(String key, double score, String member) {
Jedis jedis = getJedis();
double s = jedis.zincrby(key, score, member);
returnJedis(jedis);
return s;
}
/**
* 返回指定位置的集合元素,0为第一个元素,-1为最后一个元素
*
* @param key
* @param start 开始位置(包含)
* @param end 结束位置(包含)
* @return Set<String>
*/
public Set<String> zrange(String key, int start, int end) {
Jedis sjedis = getJedis();
Set<String> set = sjedis.zrange(key, start, end);
returnJedis(sjedis);
return set;
}
/**
* 返回指定权重区间的元素集合
*
* @param key
* @param min 上限权重
* @param max 下限权重
* @return Set<String>
*/
public Set<String> zrangeByScore(String key, double min, double max) {
Jedis sjedis = getJedis();
Set<String> set = sjedis.zrangeByScore(key, min, max);
returnJedis(sjedis);
return set;
}
/**
* 获取指定值在集合中的位置,集合排序从低到高
*
* @param key
* @param member
* @return long 位置
*/
public long zrank(String key, String member) {
Jedis sjedis = getJedis();
long index = sjedis.zrank(key, member);
returnJedis(sjedis);
return index;
}
/**
* 获取指定值在集合中的位置,集合排序从高到低
*
* @param key
* @param member
* @return long 位置
*/
public long zrevrank(String key, String member) {
Jedis sjedis = getJedis();
long index = sjedis.zrevrank(key, member);
returnJedis(sjedis);
return index;
}
/**
* 从集合中删除成员
*
* @param key
* @param member
* @return 返回1成功
*/
public long zrem(String key, String member) {
Jedis jedis = getJedis();
long s = jedis.zrem(key, member);
returnJedis(jedis);
return s;
}
/**
* 删除
*
* @param key
* @return
*/
public long zrem(String key) {
Jedis jedis = getJedis();
long s = jedis.del(key);
returnJedis(jedis);
return s;
}
/**
* 删除给定位置区间的元素
*
* @param key
* @param start 开始区间,从0开始(包含)
* @param end 结束区间,-1为最后一个元素(包含)
* @return 删除的数量
*/
public long zremrangeByRank(String key, int start, int end) {
Jedis jedis = getJedis();
long s = jedis.zremrangeByRank(key, start, end);
returnJedis(jedis);
return s;
}
/**
* 删除给定权重区间的元素
*
* @param key
* @param min 下限权重(包含)
* @param max 上限权重(包含)
* @return 删除的数量
*/
public long zremrangeByScore(String key, double min, double max) {
Jedis jedis = getJedis();
long s = jedis.zremrangeByScore(key, min, max);
returnJedis(jedis);
return s;
}
/**
* 获取给定区间的元素,原始按照权重由高到低排序
*
* @param key
* @param start
* @param end
* @return Set<String>
*/
public Set<String> zrevrange(String key, int start, int end) {
Jedis sjedis = getJedis();
Set<String> set = sjedis.zrevrange(key, start, end);
returnJedis(sjedis);
return set;
}
/**
* 获取给定值在集合中的权重
*
* @param key
* @param memebr
* @return double 权重
*/
public double zscore(String key, String memebr) {
Jedis sjedis = getJedis();
Double score = sjedis.zscore(key, memebr);
returnJedis(sjedis);
if (score != null) {
return score;
}
return 0;
}
}
/*******************************************Hash*******************************************/
public class Hash {
/**
* hmset
*
* @param key 键
* @param map 对应的哈希表键值对
* @return 状态,成功返回OK
*/
public String hmset(String key, Map<String, String> map) {
Jedis jedis = getJedis();
String s = jedis.hmset(key, map);
returnJedis(jedis);
return s;
}
/**
* hmset
*
* @param key 键
* @param map 对应的hashMap键值对
* @param time 设置过期时间
* @return 状态,成功返回OK
*/
public String hmset(String key, Map<String, String> map, int time) {
Jedis jedis = getJedis();
String s = jedis.hmset(key, map);
if (time > 0) {
expire(key, time);
}
returnJedis(jedis);
return s;
}
/**
* 以Map的形式返回哈希表中的存储和值
*
* @param key 键
* @return Map<Strinig , String>
*/
public Map<String, String> hget(String key) {
Jedis sjedis = getJedis();
Map map = sjedis.hgetAll(key);
returnJedis(sjedis);
return map;
}
/**
* 从哈希表中删除指定的值
*
* @param key hashMap的键
* @param fieid hashMap中药删除的条目的键名
* @return 状态码,1成功,0失败
*/
public long hdel(String key, String fieid) {
Jedis jedis = getJedis();
long s = jedis.hdel(key, fieid);
returnJedis(jedis);
return s;
}
/**
* 删除整个哈希表
*
* @param key hashMap的键
* @return
*/
public long hdel(String key) {
Jedis jedis = getJedis();
long s = jedis.del(key);
returnJedis(jedis);
return s;
}
/**
* 哈希表中指定的存储是否存在
*
* @param key hashMap的键
* @param fieid hashMap中条目的键
* @return 1存在,0不存在
*/
public boolean hexists(String key, String fieid) {
Jedis sjedis = getJedis();
boolean s = sjedis.hexists(key, fieid);
returnJedis(sjedis);
return s;
}
/**
* 返回哈希表中指定存储位置的值
*
* @param key
* @param fieid 存储的名字
* @return 存储对应的值
*/
public String hget(String key, String fieid) {
Jedis sjedis = getJedis();
String s = sjedis.hget(key, fieid);
returnJedis(sjedis);
return s;
}
/**
* 返回哈希表中指定存储位置的值
*
* @param key
* @param fieid 存储的名字
* @return 存储对应的值
*/
public byte[] hget(byte[] key, byte[] fieid) {
Jedis sjedis = getJedis();
byte[] s = sjedis.hget(key, fieid);
returnJedis(sjedis);
return s;
}
/**
* 哈希表添加一个对应关系
*
* @param key hashMap 键
* @param fieid hashMap中条目的键
* @param value 更新后的值
* @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
**/
public long hset(String key, String fieid, String value) {
Jedis jedis = getJedis();
long s = jedis.hset(key, fieid, value);
returnJedis(jedis);
return s;
}
/**
* 更新或增加哈希表一个条目
*
* @param key hashMap 键
* @param fieid hashMap中条目的键
* @param value 更新后的值
* @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
**/
public long hset(String key, String fieid, byte[] value) {
Jedis jedis = getJedis();
long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
returnJedis(jedis);
return s;
}
/**
* 更新或增加哈希表一个条目,只有在fieid不存在时才执行
*
* @param key hashMap 键
* @param fieid hashMap中条目的键
* @param value 更新后的值
* @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
**/
public long hsetnx(String key, String fieid, String value) {
Jedis jedis = getJedis();
long s = jedis.hsetnx(key, fieid, value);
returnJedis(jedis);
return s;
}
/**
* 获取哈希表中value的集合
*
* @param key
* @return List<String>
*/
public List<String> hvals(String key) {
Jedis sjedis = getJedis();
List<String> list = sjedis.hvals(key);
returnJedis(sjedis);
return list;
}
/**
* 哈希表中的字段值加上指定增量值
*
* @param key 哈希表的键
* @param fieid 存储位置
* @param value 要增加的值,可以是负数
* @return 增加指定数字后,存储位置的值
*/
public long hincrby(String key, String fieid, long value) {
Jedis jedis = getJedis();
long s = jedis.hincrBy(key, fieid, value);
returnJedis(jedis);
return s;
}
/**
* 返回指定hash中的所有存储名字,类似Map中的keySet方法
*
* @param key 哈希表的键
* @return Set<String> 存储名称的集合
*/
public Set<String> hkeys(String key) {
Jedis sjedis = getJedis();
Set<String> set = sjedis.hkeys(key);
returnJedis(sjedis);
return set;
}
/**
* 获取hash中存储的个数,类似Map中size方法
*
* @param key 哈希表的键
* @return long 存储的个数
*/
public long hlen(String key) {
Jedis sjedis = getJedis();
long len = sjedis.hlen(key);
returnJedis(sjedis);
return len;
}
/**
* 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
*
* @param key
* @param fieids 存储位置
* @return List<String>
*/
public List<String> hmget(String key, String... fieids) {
Jedis sjedis = getJedis();
List<String> list = sjedis.hmget(key, fieids);
returnJedis(sjedis);
return list;
}
/**
* 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
*
* @param key
* @param fieids 存储位置
* @return List<String>
*/
public List<byte[]> hmget(byte[] key, byte[]... fieids) {
Jedis sjedis = getJedis();
List<byte[]> list = sjedis.hmget(key, fieids);
returnJedis(sjedis);
return list;
}
}
/*******************************************Strings*******************************************/
public class Strings {
/**
* 普通缓存,如果记录已存在将覆盖原有的value
*
* @param key 键
* @param value 值
* @return String 操作状态
*/
public String set(String key, String value) {
Jedis jedis = getJedis();
String str = jedis.set(key, value);
returnJedis(jedis);
return str;
}
/**
* 普通缓存,如果记录已存在将覆盖原有的value
*
* @param key 键
* @param value 值
* @return String 操作状态
*/
public String set(byte[] key, byte[] value) {
Jedis jedis = getJedis();
String str = jedis.set(key, value);
returnJedis(jedis);
return str;
}
/**
* 普通缓存,添加有过期时间的记录
*
* @param key 键
* @param time 过期时间,以秒为单位
* @param value 值
* @return String 操作状态
*/
public String set(String key, String value, int time) {
Jedis jedis = getJedis();
String str = jedis.setex(key, time, value);
returnJedis(jedis);
return str;
}
/**
* 添加有过期时间的记录
*
* @param key
* @param seconds 过期时间,以秒为单位
* @param value
* @return String 操作状态
*/
public String set(byte[] key, int seconds, byte[] value) {
Jedis jedis = getJedis();
String str = jedis.setex(key, seconds, value);
returnJedis(jedis);
return str;
}
/**
* 根据key获取记录
*
* @param key
* @return 值
*/
public String get(String key) {
Jedis sjedis = getJedis();
String value = sjedis.get(key);
returnJedis(sjedis);
return value;
}
/**
* 根据key获取记录
*
* @param key
* @return 值
*/
public byte[] get(byte[] key) {
Jedis sjedis = getJedis();
byte[] value = sjedis.get(key);
returnJedis(sjedis);
return value;
}
/**
* 添加一条记录,仅当给定的key不存在时才插入
*
* @param key
* @param value
* @return long 状态码,1插入成功且key不存在,0未插入,key存在
*/
public long setnx(String key, String value) {
Jedis jedis = getJedis();
long str = jedis.setnx(key, value);
returnJedis(jedis);
return str;
}
/**
* 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/>
* 例:String str1="123456789";<br/>
* 对str1操作后setRange(key,4,0000),str1="123400009";
*
* @param key
* @param offset
* @param value
* @return long value的长度
*/
public long setRange(String key, long offset, String value) {
Jedis jedis = getJedis();
long len = jedis.setrange(key, offset, value);
returnJedis(jedis);
return len;
}
/**
* 在指定的key中追加value
*
* @param key
* @param value
* @return long 追加后value的长度
**/
public long append(String key, String value) {
Jedis jedis = getJedis();
long len = jedis.append(key, value);
returnJedis(jedis);
return len;
}
/**
* 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用
*
* @param key
* @param number 要减去的值
* @return long 减指定值后的值
*/
public long decrBy(String key, long number) {
Jedis jedis = getJedis();
long len = jedis.decrBy(key, number);
returnJedis(jedis);
return len;
}
/**
* <b>可以作为获取唯一id的方法</b><br/>
* 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用
*
* @param key
* @param number 要减去的值
* @return long 相加后的值
*/
public long incrBy(String key, long number) {
Jedis jedis = getJedis();
long len = jedis.incrBy(key, number);
returnJedis(jedis);
return len;
}
/**
* 对指定key对应的value进行截取
*
* @param key
* @param startOffset 开始位置(包含)
* @param endOffset 结束位置(包含)
* @return String 截取的值
*/
public String getrange(String key, long startOffset, long endOffset) {
Jedis sjedis = getJedis();
String value = sjedis.getrange(key, startOffset, endOffset);
returnJedis(sjedis);
return value;
}
/**
* 获取并设置指定key对应的value<br/>
* 如果key存在返回之前的value,否则返回null
*
* @param key
* @param value
* @return String 原始value或null
*/
public String getSet(String key, String value) {
Jedis jedis = getJedis();
String str = jedis.getSet(key, value);
returnJedis(jedis);
return str;
}
/**
* 批量获取记录,如果指定的key不存在返回List的对应位置将是null
*
* @param keys
* @return List<String> 值得集合
*/
public List<String> mget(String... keys) {
Jedis jedis = getJedis();
List<String> str = jedis.mget(keys);
returnJedis(jedis);
return str;
}
/**
* 批量存储记录
*
* @param keysvalues 例:keysvalues="key1","value1","key2","value2";
* @return String 状态码
*/
public String mset(String... keysvalues) {
Jedis jedis = getJedis();
String str = jedis.mset(keysvalues);
returnJedis(jedis);
return str;
}
/**
* 获取key对应的值的长度
*
* @param key
* @return value值得长度
*/
public long strlen(String key) {
Jedis jedis = getJedis();
long len = jedis.strlen(key);
returnJedis(jedis);
return len;
}
}
/*******************************************Lists*******************************************/
public class Lists {
/**
* 将List放入缓存
*
* @param key
* @param value 值
* @return 状态码
*/
public String lset(String key, List<Object> value) {
String result = set(key, value);
return result;
}
/**
* 将List放入缓存
*
* @param key
* @param value 值
* @param time 设置过期的过期时间 单位秒
* @return 状态码
*/
public String lset(String key, List<Object> value, int time) {
String result = set(key, value, time);
return result;
}
/**
* 获取缓存中List
*
* @param key
* @return 状态码
*/
public List lget(String key) {
List result = (List) get(key);
return result;
}
/**
* 覆盖操作,将覆盖List中指定位置的值
*
* @param key
* @param index 位置
* @param value 值
* @return 状态码
*/
public String lset(byte[] key, int index, byte[] value) {
Jedis jedis = getJedis();
String status = jedis.lset(key, index, value);
returnJedis(jedis);
return status;
}
/**
* 覆盖操作,将覆盖List中指定位置的值
*
* @param key
* @param index 位置
* @param value 值
* @return 状态码
*/
public String lset(String key, int index, String value) {
return lset(SafeEncoder.encode(key), index,
SafeEncoder.encode(value));
}
/**
* List长度
*
* @param key
* @return 长度
*/
public long llen(String key) {
return llen(SafeEncoder.encode(key));
}
/**
* List长度
*
* @param key
* @return 长度
*/
public long llen(byte[] key) {
Jedis sjedis = getJedis();
long count = sjedis.llen(key);
returnJedis(sjedis);
return count;
}
/**
* 获取List中指定位置的值
*
* @param key
* @param index 位置
* @return 值
**/
public String lindex(String key, int index) {
return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
}
/**
* 获取List中指定位置的值
*
* @param key
* @param index 位置
* @return 值
**/
public byte[] lindex(byte[] key, int index) {
Jedis sjedis = getJedis();
byte[] value = sjedis.lindex(key, index);
returnJedis(sjedis);
return value;
}
/**
* 将List中的第一条记录移出List
*
* @param key
* @return 移出的记录
*/
public String lpop(String key) {
return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
}
/**
* 将List中的第一条记录移出List
*
* @param key
* @return 移出的记录
*/
public byte[] lpop(byte[] key) {
Jedis jedis = getJedis();
byte[] value = jedis.lpop(key);
returnJedis(jedis);
return value;
}
/**
* 将List中最后第一条记录移出List
*
* @param key
* @return 移出的记录
*/
public String rpop(String key) {
Jedis jedis = getJedis();
String value = jedis.rpop(key);
returnJedis(jedis);
return value;
}
/**
* 向List尾部追加记录
*
* @param key
* @param value
* @return 记录总数
*/
public long lpush(String key, String value) {
return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
/**
* 向List头部追加记录
*
* @param key
* @param value
* @return 记录总数
*/
public long rpush(String key, String value) {
Jedis jedis = getJedis();
long count = jedis.rpush(key, value);
jedis.rpushx(key, value);
returnJedis(jedis);
return count;
}
/**
* 向List头部追加记录
*
* @param key
* @param value
* @return 记录总数
*/
public long rpush(byte[] key, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.rpush(key, value);
returnJedis(jedis);
return count;
}
/**
* 向List中追加记录
*
* @param key
* @param value
* @return 记录总数
*/
public long lpush(byte[] key, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.lpush(key, value);
returnJedis(jedis);
return count;
}
/**
* 获取指定范围的记录,可以做为分页使用
*
* @param key
* @param start
* @param end
* @return List
*/
public List<String> lrange(String key, long start, long end) {
Jedis sjedis = getJedis();
List<String> list = sjedis.lrange(key, start, end);
returnJedis(sjedis);
return list;
}
/**
* 获取指定范围的记录,可以做为分页使用
*
* @param key
* @param start
* @param end 如果为负数,则尾部开始计算
* @return List
*/
public List<byte[]> lrange(byte[] key, int start, int end) {
Jedis sjedis = getJedis();
List<byte[]> list = sjedis.lrange(key, start, end);
returnJedis(sjedis);
return list;
}
/**
* 删除List中c条记录,被删除的记录值为value
*
* @param key
* @param c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
* @param value 要匹配的值
* @return 删除后的List中的记录数
*/
public long lrem(byte[] key, int c, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.lrem(key, c, value);
returnJedis(jedis);
return count;
}
/**
* 删除List中c条记录,被删除的记录值为value
*
* @param key
* @param c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
* @param value 要匹配的值
* @return 删除后的List中的记录数
*/
public long lrem(String key, int c, String value) {
return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
}
/**
* 只保留start与end之间的记录
*
* @param key
* @param start 记录的开始位置(0表示第一条记录)
* @param end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
* @return 执行状态码
*/
public String ltrim(byte[] key, int start, int end) {
Jedis jedis = getJedis();
String str = jedis.ltrim(key, start, end);
returnJedis(jedis);
return str;
}
/**
* 只保留start与end之间的记录
*
* @param key
* @param start 记录的开始位置(0表示第一条记录)
* @param end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
* @return 执行状态码
*/
public String ltrim(String key, int start, int end) {
return ltrim(SafeEncoder.encode(key), start, end);
}
}
}
ConofigLoader.class
public class ConfigLoader {
/**
* 根据资源名称加载资源
* @param fileName 资源文件名称
* @return prop对象
* @throws IOException
*/
public static Properties loadProperties(String fileName) throws IOException {
InputStream is = ConfigLoader.class.getClassLoader().getResourceAsStream(fileName);
Properties prop = new Properties();
prop.load(is);
is.close();
return prop;
}
public static Properties loadProperties(InputStream is) throws IOException {
Properties prop = new Properties();
prop.load(is);
is.close();
return prop;
}
}
PropertiesUutils.class
public class PropertiesUtils {
private static Properties properties=new Properties();
static{
try {
properties = ConfigLoader.loadProperties("meiman.properties");
} catch (IOException e) {
System.out.println("加载系统配置文件错误");
}
}
/**
* 读取配置文件,如果没有读取到,就返回默认值
* @param property
* @param defaultValue
* @return
*/
public static String readProperty(String property, String defaultValue){
return properties.getProperty(property, defaultValue);
}
/**
* 读取配置文件
* @param property
* @return
*/
public static String readProperty(String property){
return properties.getProperty(property);
}
}
SerializeUtil.class用于redis存储对象时进行序列化和反序列化的操作(通俗一点讲就是存储时序列化,需要取出值时反序列化)这里注意对象实体一定要实现序列化接口!!!
public class SerializeUtil {
/**
* 序列化
* @param value
* @return
*/
public static byte[] serialize(Object value) {
if (value == null) {
return null;
}
byte[] rv = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(value);
rv = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
try {
os.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return rv;
}
/**
* 反序列化
* @param in
* @return
*/
public static Object unserialize(byte[] in) {
if (in == null) {
return null;
}
Object rv = null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
rv = is.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
is.close();
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return rv;
}
}
meiman.properties资源文件 主要是设置redis服务地址和端口号 就按照图上的填写即可
测试类 首先是存储数据 这里用到了HASH/STRING
取值
结果
大家使用时可以根据本身的需要取存储相应的数据,详情见[redis数据类型及应用场景(https://www.cnblogs.com/mrhgw/p/6278619.html)
以上就是简单的springboot整合redis的操作了,希望对你们有所帮助!!!!!!!!