网站设计杭州,手机海报制作免费软件,下载源代码建网站,铁路建设工程网站1.1 简介
1.1.1 概述 Spring Data 中有一个成员 Spring Data Redis#xff0c;他提供了 RedisTemplate 可以在 Spring 应用中更简便的访问 Redis 以及异常处理及序列化#xff0c;支持发布订阅等操作。 1.2 RedisTemplate 常见 API RedisTemplate 针对 jedis 客户端中大…1.1 简介
1.1.1 概述 Spring Data 中有一个成员 Spring Data Redis他提供了 RedisTemplate 可以在 Spring 应用中更简便的访问 Redis 以及异常处理及序列化支持发布订阅等操作。 1.2 RedisTemplate 常见 API RedisTemplate 针对 jedis 客户端中大量 API 进行了归类封装将同一类型操作封装为 operation 接口 ♞ ValueOperations 简单 string 操作 ♞ ListOperations 针对 list 类型的数据操作 ♞ HashOperations 针对 hash 即 map 类型的数据操作 ♞ SetOperations set 类型数据操作 ♞ ZSetOperations zset 类型数据操作 ☞ 示例 SpringBootTest
public class RedisTest {Autowiredprivate RedisTemplate redisTemplate;Testpublic void redis() {redisTemplate.opsForValue().set(name, 张三);Object name redisTemplate.opsForValue().get(name);System.out.println(name);}
}
1.2.2 BoundKeyOperations RedisTemplate 提供了对 key 的 bound(绑定) 便捷化操作 API可以通过 bound 封装指定的 key然后进行一系列的操作而无须显式的再次指定 Key。 ♞ BoundValueOperations 绑定 string 类型的 key ♞ BoundListOperations 绑定 list 类型的 key ♞ BoundHashOperations 绑定 hash 即 map 类型的 key ♞ BoundSetOperations 绑定 set 类型的 key ♞ BoundZSetOperations 绑定 zset 类型的 key ☞ 示例 SpringBootTest
public class RedisTest {Autowiredprivate RedisTemplate redisTemplate;Testpublic void redis() {Object name redisTemplate.boundValueOps(name).get();System.out.println(name);}
}1.3 数据操作
1.3.1 通用方法
通用方法删除 key
// 删除单个 key返回布尔值
redisTemplate.delete(K key);// 删除多个 key返回删除的个数
redisTemplate.delete(CollectionK keys);通用方法判断 key 是否存在
// 返回布尔值
redisTemplate.hasKey(key);通用方法 key 有效时间
// 指定有效时间
redisTemplate.expire(key, time, TimeUnit.MINUTES);// 获取有效时间返回值单位为秒
redisTemplate.getExpire(key);1.3.2 操作 string
string类型添加数据
// 通过 ValueOperations 设置值
ValueOperations ops redisTemplate.opsForValue();
// 存入数据
ops.set(key, value);
// 设置过期时间
ops.set(key, value, time, TimeUnit.SECONDS); // 通过 BoundValueOperations 设置值
BoundValueOperations key redisTemplate.boundValueOps(key);
key.set(value);
key.set(value, time, TimeUnit.SECONDS);string类型获取数据
// 通过 ValueOperations 获取值
redisTemplate.opsForValue().get(key);// 通过 BoundValueOperations 获取值
redisTemplate.boundValueOps(key).get();1.3.3 操作 list
list类型 添加数据
// 通过 ValueOperations 设置值
ListOperations opsList redisTemplate.opsForList();
opsList.leftPush(listKey, listLeftValue);
opsList.rightPush(listKey, listRightValue);
// 存入集合
opsList.rightPushAll(list);
opsList.leftPushAll(list);// BoundValueOperations 操作类似list类型 获取数据
// 获取集合中的数据
redisTemplate.boundListOps(listKey).range(startIndex, endindex); // 根据索引获取数据
redisTemplate.boundListOps(listKey).index(index);// 集合长度
redisTemplate.boundListOps(listKey).size();list类型 删除数据
// 从左侧弹出一个元素并返回
redisTemplate.boundListOps(listKey).leftPop(); // 从右侧弹出一个元素并返回
redisTemplate.boundListOps(listKey).rightPop(); // 移出 N 个值为 value 的元素
redisTemplate.boundListOps(listKey).remove(long, value); list类型修改数据
// 根据索引修改数据
redisTemplate.boundListOps(listKey).set(index, listLeftValue);1.3.4 hash
hash类型添加数据
// 通过 BoundValueOperations 设置值
BoundHashOperations hashKey redisTemplate.boundHashOps(HashKey);
hashKey.put(key, Vaue);
// 添加一个集合
hashKey.putAll(hashMap); // 通过 ValueOperations 设置值
HashOperations hashOps redisTemplate.opsForHash();
hashOps.put(HashKey, key, Vaue);hash类型获取数据
// 获取所有小 key
redisTemplate.boundHashOps(HashKey).keys();// 根据小 key 获取值
redisTemplate.boundHashOps(HashKey)get(key);// 获取所有键值对集合
redisTemplate.boundHashOps(HashKey).entries();hash类型删除数据
// 判断 hash 中是否存在小 key
redisTemplate.boundHashOps(HashKey).hasKey(key);// 根据小 key 删除值
redisTemplate.boundHashOps(HashKey).delete(key);1.3.5 set
hash类型 添加数据
// 通过 BoundValueOperations 设置值
redisTemplate.boundSetOps(setKey).add(setValue1, setValue2, setValue3);// 通过 ValueOperations 设置值
redisTemplate.opsForSet().add(setKey, SetValue1, setValue2, setValue);hash类型 获取数据
// 获取所有值
redisTemplate.boundSetOps(setKey).members();// 获取 set 的长度
redisTemplate.boundSetOps(setKey).size();hash类型删除数据
// 判断 set 中是否存在改值
redisTemplate.boundSetOps(setKey).isMember(setValue);// 移出指定的值
redisTemplate.boundSetOps(setKey).remove(setValue);1.3.6 zset
Zset类型 添加数据
// 通过 BoundValueOperations 设置值
redisTemplate.boundZSetOps(zSetKey).add(zSetVaule, score);// 通过 ValueOperations 设置值
redisTemplate.opsForZSet().add(zSetKey, zSetVaule, score);Zset类型获取数据
// 获取元素集合, 按照排名先后(从小到大)
redisTemplate.boundZSetOps(zSetKey).range(key, startIndex, endIndex);// 获取指定值的分数(权重)
redisTemplate.boundZSetOps(zSetKey).score(zSetVaule);// 获取 zset 长度
redisTemplate.boundZSetOps(zSetKey).size();Zset类型修改分数
// 修改指定元素的分数
redisTemplate.boundZSetOps(zSetKey).incrementScore(zSetVaule, score);Zset类型删除数据
// 删除指定元素
redisTemplate.boundZSetOps(zSetKey).remove(zSetVaule);// 删除指定索引范围的元素
redisTemplate.boundZSetOps(zSetKey).removeRange(strat, end);// 删除指定分数范围的元素
redisTemplate.boundZSetOps(zSetKey).removeRangeByScorssse(strat, end); 文章转载自: http://www.morning.ykrg.cn.gov.cn.ykrg.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn http://www.morning.mkpkz.cn.gov.cn.mkpkz.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.gchqy.cn.gov.cn.gchqy.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.bxch.cn.gov.cn.bxch.cn http://www.morning.pzlhq.cn.gov.cn.pzlhq.cn http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.trfh.cn.gov.cn.trfh.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.pwdgy.cn.gov.cn.pwdgy.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.qbkw.cn.gov.cn.qbkw.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.rpgdd.cn.gov.cn.rpgdd.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.krlsz.cn.gov.cn.krlsz.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn