好处:
下载:https://github.com/MSOpenTech/redis/releases
使用:先点击 redis-server.exe 打开服务,再点击,redis-cli.exe就可以打开客户端,进行,get,set操作了
先说目前java非常适合的缓存工具jedis,超级无脑简单工具类
要求:下载适配版本安装即可,打开服务,使用的话加载依赖,合理配置调试就ok
1.加载依赖:
redis.clients
jedis
2.properties配置环境
redis.host=127.0.0.1
redis.port=6379
3.工具类,现成的,拿来主义固然简单,如果自己去一个一个推,印象会更深刻哟!
package com.meiman.meimanwallet.utils;
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);
//表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(1000 * 100);
//在borrow一个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);
}
/*******************************************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;
}
/**
* 获取给定key中元素个数
*
* @param key
* @return 元素个数
*/
public long scard(String key) {
Jedis sjedis = getJedis();
long len = sjedis.scard(key);
returnJedis(sjedis);
return len;
}
/**
* 返回从第一组和所有的给定集合之间的差异的成员
*
* @param keys
* @return 差异的成员集合
*/
public Set sdiff(String... keys) {
Jedis jedis = getJedis();
Set 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 sinter(String... keys) {
Jedis jedis = getJedis();
Set 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;
}
/**
* 返回集合中的所有成员
*
* @param key
* @return 成员集合
*/
public Set smembers(String key) {
Jedis sjedis = getJedis();
Set set = sjedis.smembers(key);
returnJedis(sjedis);
return set;
}
public Set smembers(byte[] key) {
Jedis sjedis = getJedis();
Set set = sjedis.smembers(key);
returnJedis(sjedis);
return set;
}
/**
* 将成员从源集合移出放入目标集合
* 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0
* 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
*
* @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;
}
/**
* 合并多个集合并返回合并后的结果,合并后的结果集合并不保存
*
* @param keys
* @return 合并后的结果集合
*/
public Set sunion(String... keys) {
Jedis jedis = getJedis();
Set 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 long zcard(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;
}
/**
* 获得set的长度
*
* @param key
* @return
*/
public long zlength(String key) {
long len = 0;
Set set = zrange(key, 0, -1);
len = set.size();
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
*/
public Set zrange(String key, int start, int end) {
Jedis sjedis = getJedis();
Set set = sjedis.zrange(key, start, end);
returnJedis(sjedis);
return set;
}
/**
* 返回指定权重区间的元素集合
*
* @param key
* @param min 上限权重
* @param max 下限权重
* @return Set
*/
public Set zrangeByScore(String key, double min, double max) {
Jedis sjedis = getJedis();
Set 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
*/
public Set zrevrange(String key, int start, int end) {
Jedis sjedis = getJedis();
Set 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 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 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
*/
public Map 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
*/
public List hvals(String key) {
Jedis sjedis = getJedis();
List 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 存储名称的集合
*/
public Set hkeys(String key) {
Jedis sjedis = getJedis();
Set 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
*/
public List hmget(String key, String... fieids) {
Jedis sjedis = getJedis();
List list = sjedis.hmget(key, fieids);
returnJedis(sjedis);
return list;
}
/**
* 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
*
* @param key
* @param fieids 存储位置
* @return List
*/
public List hmget(byte[] key, byte[]... fieids) {
Jedis sjedis = getJedis();
List list = sjedis.hmget(key, fieids);
returnJedis(sjedis);
return list;
}
}
/*******************************************Strings*******************************************/
public class Strings {
/**
* 普通缓存,添加记录
*
* @param key 键
* @param seconds 过期时间,以秒为单位
* @param value 值
* @return String 操作状态
*/
public String set(String key, int seconds, String value) {
Jedis jedis = getJedis();
String str = jedis.setex(key, seconds, value);
returnJedis(jedis);
return str;
}
/**
* 普通缓存,添加有过期时间的记录
*
* @param key 键
* @param seconds 过期时间,以秒为单位
* @param value 值
* @return String 操作状态
*/
public String setEx(String key, int seconds, String value) {
Jedis jedis = getJedis();
String str = jedis.setex(key, seconds, value);
returnJedis(jedis);
return str;
}
/**
* 添加有过期时间的记录
*
* @param key
* @param seconds 过期时间,以秒为单位
* @param value
* @return String 操作状态
*/
public String setEx(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;
}
/**
* 添加记录,如果记录已存在将覆盖原有的value
*
* @param key
* @param value
* @return 状态码
*/
public String set(String key, String value) {
return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
/**
* 添加记录,如果记录已存在将覆盖原有的value
*
* @param key
* @param value
* @return 状态码
*/
public String set(String key, byte[] value) {
return set(SafeEncoder.encode(key), value);
}
/**
* 添加记录,如果记录已存在将覆盖原有的value
*
* @param key
* @param value
* @return 状态码
*/
public String set(byte[] key, byte[] value) {
Jedis jedis = getJedis();
String status = jedis.set(key, value);
returnJedis(jedis);
return status;
}
/**
* 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据
* 例:String str1="123456789";
* 对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;
}
/**
* 可以作为获取唯一id的方法
* 将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
* 如果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 值得集合
*/
public List mget(String... keys) {
Jedis jedis = getJedis();
List 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
* @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 位置
* @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));
}
/**
* 在value的相对位置插入记录
*
* @param key
* @param where 前面插入或后面插入
* @param pivot 相对位置的内容
* @param value 插入的内容
* @return 记录总数
*/
/* public long linsert(String key, BinaryClient.LIST_POSITION where, String pivot, String value) {
return linsert(SafeEncoder.encode(key), where,
SafeEncoder.encode(pivot), SafeEncoder.encode(value));
}*/
/**
* 在指定位置插入记录
*
* @param key
* @param where 前面插入或后面插入
* @param pivot 相对位置的内容
* @param value 插入的内容
* @return 记录总数
*/
public long linsert(byte[] key, BinaryClient.LIST_POSITION where, byte[] pivot, byte[] value) {
Jedis jedis = getJedis();
long count = jedis.linsert(key, where, pivot, value);
returnJedis(jedis);
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);
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 lrange(String key, long start, long end) {
Jedis sjedis = getJedis();
List list = sjedis.lrange(key, start, end);
returnJedis(sjedis);
return list;
}
/**
* 获取指定范围的记录,可以做为分页使用
*
* @param key
* @param start
* @param end 如果为负数,则尾部开始计算
* @return List
*/
public List lrange(byte[] key, int start, int end) {
Jedis sjedis = getJedis();
List 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);
}
}
}
4.简单测试:测试类里面操作的。主要是做一个设置和取值
public class UtilTest {
@Test
public void testTime(){
System.out.println(LongUtils.div(System.currentTimeMillis(),100000000));
}
@Test
public void redisUtilSetTest()
{
HashMap map=new HashMap();
map.put("xiaowang","66");
map.put("zhangsan","88");
map.put("lisi","86");
map.put("wangwu","99");
map.put("wangmazi","98");
RedisUtil.getInstance().HASH.hmset("student",map,60);
RedisUtil.getInstance().STRINGS.set("bl","123");
String bl = RedisUtil.getInstance().STRINGS.get("bl");
System.out.println(bl);
}
@Test
public void redisUtilGetTest()
{
Map map=RedisUtil.getInstance().HASH.hget("student");
for(String key :map.keySet())
{
System.out.println(key+":"+map.get(key));
}
}
}
5.其实相较redis,redis是会多一点配置,依赖包也不一样,jedis则是相较redis,做了一个封装,很适合当前的java
org.springframework.boot
spring-boot-starter-data-redis
6.配置文件:
application.yml的话:
testName:
applicationName: testRedis
spring:
redis:
host: 127.0.0.1
port: 6379
redis.properties的话:
Redis数据库索引(默认为0)
spring.redis.database=0# Redis服务器地址
spring.redis.host=127.0.0.1
Redis服务器连接端口
spring.redis.port=6379
Redis服务器连接密码(默认为空)
spring.redis.password=
连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
连接池中的最大空闲连接
spring.redis.pool.max-idle=8
连接池中的最小空闲连接
spring.redis.pool.min-idle=0
连接超时时间(毫秒)
spring.redis.timeout=0
7.redisConfig 配置文件,主要是RedisTemplate 和 RedisConnectionFactory
8.redisUtils工具类文件,编写
9.调用。就和jedis是差不多的了!
。。。。。。哈哈,redis不想写了,可以百度,应该蛮多的。作为一个javaDog 建议jedis,挺香的!
加油吧!