目录
- Jedis的API使用测试
- Jedis建立连接与测试
- Jedis常用API
- jedis事务测试
- Redis与Springboot的整合
Jedis的API使用测试
Jedis是官方指定的Redis连接工具,如果想在java中使用Redis,应该和MySQL一样需要有类似jdbc一样的工具来建立连接并且操作。而Jedis的作用就类似于jdbc。在数据库连接和操作中是必不可少的。
Jedis建立连接与测试
- 导入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
- 测试连接
@Test
void contextLoads() {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.connect(); // 也可不用手动连接,发出第一个请求时会自动连接。
System.out.println(jedis.ping()); // 测试是否连通,也可使用jedis.isConnected()方法;
jedis.set("name", "zhangsan");
System.out.println(jedis.get("name"));
jedis.disconnect(); // 销毁连接
}
Jedis常用API
在输入框中输入jedis.
会发现,jedis的方法完美的复现了我们在redis.client的操作。我们的每一个操作在jedis中都有方法与之相对应。这里对常用的方法进行一个简单实践。
Key操作
/**
* 通用方法
*/
@Test
void contextLoads0() {
Jedis jedis = new Jedis("127.0.0.1",6379);
// key * 查看所有键
System.out.println(jedis.keys("*"));
// exists key 判断key是否存在
System.out.println(jedis.exists("name"));
// move key db 移动key至指定db
System.out.println(jedis.move("name",1));
// del key 删除键
jedis.del("name");
// expire key second 设置过期时间
System.out.println(jedis.expire("name", 20));
// ttl key 查看剩余过期时间
System.out.println(jedis.ttl("name"));
jedis.disconnect();
}
String操作
@Test
void contextLoads() {
Jedis jedis = new Jedis("127.0.0.1",6379);
// set key value 设置值
jedis.set("name","张三");
// get key 获取值
System.out.println(jedis.get("name"));
// mset key value [key value ...] 批量设置值
jedis.mset("name","张三","age","18","addr","中国云南");
// mget key [key ...] 批量获取
System.out.println(jedis.mget("name", "age", "addr"));
// setex keY seconds values (set and expire) 设置值并设置时间
// setnx key value (set if no exists) 设置值当不存在时
jedis.setnx("name", "张三");
jedis.setex("name", 10, "王五");
// msetnx 批量设置时判断是否存在
// 原子性操作,一个存在全部失败。
jedis.msetnx("k1","v1","k2","v2");
// getset key value 先过去再更新值,不存在时set,存在时修改
jedis.getSet("name", "王五");
jedis.set("age", "12");
// incr key 增加值1
// decr key 减少值1
// incrby key increment 增加指定值
// decrby key increment 减少指定值
jedis.incr("age");
jedis.incrBy("age", 12);
jedis.decr("age");
jedis.decrBy("age", 10);
// strlen key 判断字符长度
jedis.strlen("name");
// exists key [key...] 判断字符是否存在
jedis.exists("age");
// append key value 在字符后面添加
// 若不村在,直接创建
jedis.append("name", "123");
jedis.append("aha", "123");
// setrange key offset value 在指定下标开始替换
jedis.setrange("name", 2, "123");
// getrange key start end 指定范围获取
jedis.getrange("name", 1, 3);
jedis.disconnect();
}
List操作
@Test
void contestLoads1() {
Jedis jedis = new Jedis("127.0.0.1",6379);
List<String> list = new ArrayList<>();
list.add("hello");
list.get(1);
// lpush key value [value] 从左边(头部)插入元素
// rpush key value [value...] 从右边(尾部)插入元素
// lrange key start end 获取指定下标范围的值
jedis.lpush("list", "1","2","3","4");
jedis.rpush("list", "a","b","c","d");
jedis.lrange("list", 0,-1); // 获取全部
// lpop key # 从左边(头部)弹出值
jedis.lpop("list");
jedis.rpop("list");
// lset key index value # 更新指定下标的值
jedis.lset("list", 1, "lset");
// lindex key index 获取指定下标的值
jedis.lindex("list", 1);
// linsert key BEFORE|AFTER pivot value # 在指定值前面或者后面添加值。
jedis.linsert("list", ListPosition.AFTER,"b","isyou");
//llen key 返回列表长度
jedis.llen("list");
// lrem key count value # 删除指定个数的指定值
jedis.lrem("list",2, "b");
// ltrim key start end # 指定范围裁剪
jedis.ltrim("list", 1, 2);
// rpoplpush source destination # 移除指定数组最后以为到目标数组第一位
jedis.rpoplpush("list", "list1");
jedis.disconnect();
}
Set操作
@Test
void contestLoads2() {
Jedis jedis = new Jedis("127.0.0.1",6379);
Set<String> set = new HashSet<>();
set.add("a");
set.clear();
// sadd key member [member...] # 添加一个或多个值(非重复)
jedis.sadd("set", "a","b","c","d");
// smembers key # 获取集合内的全部元素
jedis.smembers("set");
// scard key # 获取集合个数
jedis.smembers("set");
// srem key member [member...] # 移除指定元素
jedis.srem("set", "a");
// srandmember key count # 随机返回集合内指定个数元素
// 默认为1
jedis.srandmember("set",2);
// 随机弹出集合内一个元素
jedis.spop("set");
// smove source destination member
jedis.smove("set", "set2", "a");
// sdiff key [key...] # 查看差集
// sinter key [key...] # 查看交集
// sunion key [key...] # 查看并集
jedis.sdiff("set","set1");
jedis.sinter("set","set1");
jedis.sunion("set","set1");
jedis.disconnect();
}
Hash操作
@Test
void contestLoads3() {
Jedis jedis = new Jedis("127.0.0.1",6379);
Map<String, Object> map = new HashMap<>();
map.put("message", "world");
map.get("message");
// hset key filed value # 设置值
// hset key filed # 获取值
jedis.hset("map","name","张三");
jedis.hget("map", "name");
// hmset key Map<String String> 批量设置值
// hmget key filed value [filed value...] 批量获取值
Map<String,String> map1 = new HashMap<>();
map1.put("age", "12");
map1.put("addr", "云南");
jedis.hmset("map",map1);
List<String> hmget = jedis.hmget("map", "name", "age");
// hgetall key # 获取全部域和值
// hkeys key # 获取某个key的全部域
// hvals key # 获取全部域的值
// hdel filed # 删除指定域
jedis.hgetAll("map");
jedis.hkeys("map");
jedis.hvals("map");
jedis.hdel("map", "addr");
// hsetnx key filed value # 当域不存在时设置
jedis.hsetnx("map", "addr", "中国江西");
// hincrby key filed increment # 指定域增长指定值
jedis.hincrBy("map", "age", 12);
jedis.disconnect();
}
Zset操作
@Test
void contestLoads4() {
Jedis jedis = new Jedis("127.0.0.1",6379);
// zset key scores member 添加
// zrange key start stop [withscores] 指定范围内[下标]查看(升序)
// zrerange key start stop [withscores] 指定范围内[下标]查看(降序)
jedis.zadd("salary", 11000,"小明");
jedis.zadd("salary", 11200,"小红");
jedis.zadd("salary", 12000,"小忘");
jedis.zrange("salary", 0,-1);
jedis.zrevrange("salary", 0,-1);
// zrem key member [member] 移除指定元素
jedis.zrem("salary", "小明","小红");
// zcard salary 统计集合内元素个数
jedis.zcard("salary");
// zcount key min max 统计某一区间的元素
jedis.zcount("salary", 10000, 11000);
// zrank key member # 返回某一元素在集合内的排名(升序)
// zrank key member # 返回某一元素在集合内的排名(降序)
jedis.zrank("salary", "小明");
// zrangebyscore key min max [withscores] 指定区间排序值(升序)
jedis.zrangeByScoreWithScores("salary", 10000, 12000);
jedis.disconnect();
}
Geo操作
@Test
void contestLoads5() {
Jedis jedis = new Jedis("127.0.0.1",6379);
// geoadd key longitude latitude member 添加某一地址的经纬度
// geoadd key Map<String, GeoCoordinate> 批量添加多地址经纬度
jedis.geoadd("china",120.21, 30.20,"杭州");
Map<String, GeoCoordinate> map = new HashMap<>();
map.put("上海", new GeoCoordinate(121.48, 31.40));
map.put("深圳", new GeoCoordinate(113.23, 40.22));
jedis.geoadd("china",map);
// geopos key member # 获取经纬度
jedis.geopos("china", "上海");
// geodist key member1 member2 [unit] # 获取两地距离
jedis.geodist("china", "上海", "杭州");
jedis.geodist("china", "上海", "杭州",GeoUnit.KM);
// georadius key longitude latitude radius m|km|ft|mi
// 给定经纬度为中心的一定半径内的地理坐标
jedis.georadius("china", 120, 30, 1000, GeoUnit.KM);
// georadiusbymember key member radius m|km
jedis.georadiusByMember("china", "杭州", 1000, GeoUnit.KM);
// geohash member [member1...] 返回某地经纬度的hash值,越相似越接近。
jedis.geohash("china", "杭州");
// geo底层时zset,使用zset方法
//==============================
jedis.zrange("china", 0, -1);
jedis.zrem("china", "杭州");
jedis.disconnect();
}
HyperLogLog操作
@Test
void contestLoads6() {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.connect();
// pfadd key element [element...] 添加元素
// pfcount key 统计元素个数
// pfmerge key destkey sourcekey [sourcekey] 合并key
jedis.pfadd("pf", "a","b","c","c");
jedis.pfcount("pf");
jedis.pfadd("pf1", "d","e");
jedis.pfmerge("pf", "pf1");
jedis.disconnect();
}
BitMap
@Test
void contestLoads7() {
Jedis jedis = new Jedis("127.0.0.1",6379);
// setbit key offset value # 设置值
// getbit key offset # 获取值
// bitcount key [start end] # 统计一定区间的数量
jedis.setbit("sign", 0, "0");
jedis.setbit("sign", 1, "1");
System.out.println("打卡情况为:"+jedis.getbit("sign", 0));
System.out.println("个数为:"+jedis.bitcount("sign",0,1));
jedis.disconnect();
}
jedis事务测试
@Test
void contestLoads8(){
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.set("name", "张三");
Transaction multi = jedis.multi();
try {
multi.set("name", "李四");
multi.set("age", "12");
int i = 100/0;
multi.exec();
}catch (Exception e){
multi.discard();
}finally {
System.out.println(jedis.get("name"));
System.out.println(jedis.get("age"));
jedis.close();
}
}
Redis与Springboot的整合
- 在springboot2.x之后,springboot将原有的jedis更换为lettuce,使用redis-starter需要lettuce的支持。
- jedis默认采用直连的方式,多线程下是不安全的,而要保证线程安全需要使用jedispool连接池,是一种BIO阻塞式的框架。
- lettce使用netty高性能网络框架,实例可以在多个线程中共享。
在SpringBoot的AotuConfig默认提供了RedisTemplate操作Redis
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(name = {"redisTemplate"})
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
// 默认的redisTemplate缺少一些设置,redis对象没有进行序列化
// 更改为<String, Object>不需转换
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
// String最为常用
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
整合
- 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.5.2</version>
</dependency>
- 配置redis(详情配置可以查看其配置文件)
spring.redis.host=127.0.0.1
spring.redis.port=6379
- 测试
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads9(){
redisTemplate.opsForValue().set("name", "王五nb");
System.out.println(redisTemplate.opsForValue().get("name"));
// 有些方法需要获取当前数据库连接才可以是使用,如刷新
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.flushDb();
}
自定义RedisTemplate
在原生的RedisTemplate中,默认使用jdk的序列化方式,而如果想要更换序列化方式,需要配置自定义的RedisTemplate。
在实际应用中,虽然比较少使用直接存储对象的方式,并且对象一般都会实现序列化接口,但是依然需要提供一个统一的序列化方式来防止意外情况发生,不然没有实现序列化的对象将无法存储与传输。
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
自定义Redis工具类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public final class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 指定缓存失效时间
* @param key 键
* @param time 时间(秒)
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/***
* 根据key 获取过期时间
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/***
* 判断key是否存在
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/***
* 删除缓存
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
}
}
}
/**
* 普通缓存获取
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/***
* 普通缓存放入
* @param key 键
* @param value 值
* @return true成功
* false失败
**/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/*** 普通缓存放入并设置时间 * @param key 键 * @param value 值 * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 * @return true成功 false 失败 */
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
* @param key 键 *
* @param delta 要增加几(大于0)
* */
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
* @param key 键
* @param delta 要减少几(小于0)
* */
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
/**
* HashGet
* @param key 键 不能为null
* @param item 项 不能为null
**/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
* @param key 键
* @return 对应的多个键值
**/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
* @param key 键
* @param map 对应多个键值
**/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并设置时间
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
* */
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
**/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
**/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除hash表中的值
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
**/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断hash表中是否有该项的值
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
**/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
**/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/*
* hash递减
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
**/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
/**
* 根据key获取Set中的所有值
* @param key 键
**/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据value从一个set中查询,是否存在
* @param key 键
* @param value 值
* @return true 存在 false不存在
**/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将数据放入set缓存
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
**/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 将set数据放入缓存
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
* */
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取set缓存的长度** @param key 键
**/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值为value的
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
* */
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取list缓存的内容
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* */
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取list缓存的长度
* @param key 键
* */
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通过索引 获取list中的值
* @param key 键
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0 时,-1,表尾,-2倒数第二个元素,依次类推
**/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将list放入缓存
* @param key 键
* @param value 值
* */
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
* @param key 键
* @param value 值
* @param time 时间(秒)
* */
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
* @param key 键
* @param value 值
* @return
**/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return */
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据索引修改list中的某条数据
* @param key 键
* @param index 索引
* @param value 值
* @return
* */
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N个值为value
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
**/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}