搭建springboot项目

application.yml

server:port: 9001
spring:redis:host: 127.0.0.1port: 6379jedis:pool:max-wait: 30000 #连接池最大阻塞等待时间,使用负值表示没有限制max-active: 100 #连接池最大连接数,使用负值表示没有限制max-idle: 20 #连接池中的最大空闲连接min-idle: 0 #连接池中的最小空闲连接timeout: 3000 #连接超时

pom.xml

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId><version>1.4.1.RELEASE</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><dependency><groupId>com.touch</groupId><artifactId>springtest</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency></dependencies>

RedisConfig.java

@Configuration
public class RedisConfig extends CachingConfigurerSupport{@Bean@SuppressWarnings("all")public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();template.setConnectionFactory(redisConnectionFactory);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序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}
}

RedisUtils.java

@Component
public class RedisUtils {@Autowiredprivate RedisTemplate redisTemplate;public RedisUtils(RedisTemplate<String, Object> redisTemplate) {this.redisTemplate = redisTemplate;}/*** 写入缓存* @param key* @param value* @return*/public boolean set(final String key, Object value) {boolean result = false;try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();operations.set(key, value);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** 写入缓存设置时效时间* @param key* @param value* @return*/public boolean set(final String key, Object value, Long expireTime ,TimeUnit timeUnit) {boolean result = false;try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();operations.set(key, value);redisTemplate.expire(key, expireTime, timeUnit);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** 批量删除对应的value* @param keys*/public void remove(final String... keys) {for (String key : keys) {remove(key);}}/*** 批量删除key* @param pattern*/public void removePattern(final String pattern) {Set<Serializable> keys = redisTemplate.keys(pattern);if (keys.size() > 0){redisTemplate.delete(keys);}}/*** 删除对应的value* @param key*/public void remove(final String key) {if (exists(key)) {redisTemplate.delete(key);}}/*** 判断缓存中是否有对应的value* @param key* @return*/public boolean exists(final String key) {return redisTemplate.hasKey(key);}/*** 读取缓存* @param key* @return*/public Object get(final String key) {Object result = null;ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();result = operations.get(key);return result;}/*** 哈希 添加* @param key* @param hashKey* @param value*/public void hmSet(String key, Object hashKey, Object value){HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();hash.put(key,hashKey,value);}/*** 哈希获取数据* @param key* @param hashKey* @return*/public Object hmGet(String key, Object hashKey){HashOperations<String, Object, Object>  hash = redisTemplate.opsForHash();return hash.get(key,hashKey);}/*** 列表添加* @param k* @param v*/public void lPush(String k,Object v){ListOperations<String, Object> list = redisTemplate.opsForList();list.rightPush(k,v);}/*** 列表获取* @param k* @param l* @param l1* @return*/public List<Object> lRange(String k, long l, long l1){ListOperations<String, Object> list = redisTemplate.opsForList();return list.range(k,l,l1);}/*** 集合添加* @param key* @param value*/public void add(String key,Object value){SetOperations<String, Object> set = redisTemplate.opsForSet();set.add(key,value);}/*** 集合获取* @param key* @return*/public Set<Object> setMembers(String key){SetOperations<String, Object> set = redisTemplate.opsForSet();return set.members(key);}/*** 有序集合添加* @param key* @param value* @param scoure*/public void zAdd(String key,Object value,double scoure){ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();zset.add(key,value,scoure);}/*** 有序集合获取* @param key* @param scoure* @param scoure1* @return*/public Set<Object> rangeByScore(String key, double scoure, double scoure1){ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();return zset.rangeByScore(key, scoure, scoure1);}/*** 设置key字段第offset位bit数值** @param key    字段* @param offset 位置* @param value  数值*/public void setBit(String key, long offset, boolean value) {redisTemplate.execute((RedisCallback) con -> con.setBit(key.getBytes(), offset, value));}/*** 判断该key字段offset位否为1** @param key    字段* @param offset 位置* @return 结果*/public boolean getBit(String key, long offset) {return (boolean) redisTemplate.execute((RedisCallback) con -> con.getBit(key.getBytes(), offset));}/*** 统计key字段value为1的总数** @param key 字段* @return 总数*/public Long bitCount(String key) {return (Long) redisTemplate.execute((RedisCallback<Long>) con -> con.bitCount(key.getBytes()));}/*** 统计key字段value为1的总数,从start开始到end结束** @param key   字段* @param start 起始* @param end   结束* @return 总数*/public Long bitCount(String key, Long start, Long end) {return (Long) redisTemplate.execute((RedisCallback) con -> con.bitCount(key.getBytes(), start, end));}/*** 取多个key并集并计算总数** @param key key* @return 总数*/public Long OpOrCount(String... key) {byte[][] keys = new byte[key.length][];for (int i = 0; i < key.length; i++) {keys[i] = key[i].getBytes();}redisTemplate.execute((RedisCallback) con -> con.bitOp(RedisStringCommands.BitOperation.OR, (key[0] + "To" + key[key.length - 1]).getBytes(), keys));redisTemplate.expire(key[0] + "To" + key[key.length - 1], 10, TimeUnit.SECONDS);return bitCount(key[0] + "To" + key[key.length - 1]);}/*** 取多个key的交集并计算总数** @param key key* @return 总数*/public Long OpAndCount(String... key) {byte[][] keys = new byte[key.length][];for (int i = 0; i < key.length; i++) {keys[i] = key[i].getBytes();}redisTemplate.execute((RedisCallback) con -> con.bitOp(RedisStringCommands.BitOperation.AND, (key[0] + "To" + key[key.length - 1]).getBytes(), keys));redisTemplate.expire(key[0] + "To" + key[key.length - 1], 10, TimeUnit.SECONDS);return bitCount(key[0] + "To" + key[key.length - 1]);}/*** 取多个key的补集并计算总数** @param key key* @return 总数*/public Long OpXorCount(String... key) {byte[][] keys = new byte[key.length][];for (int i = 0; i < key.length; i++) {keys[i] = key[i].getBytes();}redisTemplate.execute((RedisCallback) con -> con.bitOp(RedisStringCommands.BitOperation.XOR, (key[0] + "To" + key[key.length - 1]).getBytes(), keys));redisTemplate.expire(key[0] + "To" + key[key.length - 1], 10, TimeUnit.SECONDS);return bitCount(key[0] + "To" + key[key.length - 1]);}/*** 取多个key的否集并计算总数** @param key key* @return 总数*/public Long OpNotCount(String... key) {byte[][] keys = new byte[key.length][];for (int i = 0; i < key.length; i++) {keys[i] = key[i].getBytes();}redisTemplate.execute((RedisCallback) con -> con.bitOp(RedisStringCommands.BitOperation.NOT, (key[0] + "To" + key[key.length - 1]).getBytes(), keys));redisTemplate.expire(key[0] + "To" + key[key.length - 1], 10, TimeUnit.SECONDS);return bitCount(key[0] + "To" + key[key.length - 1]);}public List<Long> bitField(String buildSignKey,int limit,int offset){return (List<Long>)redisTemplate.execute((RedisCallback<List<Long>>) con ->con.bitField(buildSignKey.getBytes(),BitFieldSubCommands.create().get(BitFieldSubCommands.BitFieldType.unsigned(limit)).valueAt(offset)));}public Long setHyperLogLog(String key,Object...values){return redisTemplate.opsForHyperLogLog().add(key,values);}public Long getHyperLogLog(Object...keys){return redisTemplate.opsForHyperLogLog().size(keys);}public BoundGeoOperations setGeo(Object key){return redisTemplate.boundGeoOps(key);}
}

RedisController.java

@RestController
@RequestMapping("/redis")
@Slf4j
public class RedisController {@Resourceprivate RedisUtils redisUtils;@RequestMapping("/set")public void redisSet(){//StringredisUtils.set("String","中文");List<String> list = new ArrayList<>();list.add("a");list.add("b");list.add("c");redisUtils.set("list",list);redisUtils.set("user2",new User("210123","阿丹",25));//hashredisUtils.hmSet("user1","userhash",new User("210123","阿丹",25));User user = (User)redisUtils.hmGet("user1","userhash");log.info("hash-object:{}",user);//列表添加redisUtils.lPush("list1","a");redisUtils.lPush("list1","b");List<Object> listValue = redisUtils.lRange("list1",0,list.size());log.info("listValue-object:{}",listValue);//集合添加Map<String,User> map = new HashMap<>();map.put("adan",new User("210123","阿丹",25));map.put("adana",new User("210123","阿丹a",18));redisUtils.add("collection",map);Set<Object> set = redisUtils.setMembers("collection");log.info("setValue-object:{}",set);//有序集合redisUtils.zAdd("collection-order","18",1);redisUtils.zAdd("collection-order","12",2);redisUtils.zAdd("collection-order","14",4);redisUtils.zAdd("collection-order","123",3);Set<Object> zset = redisUtils.rangeByScore("collection-order",1,5);log.info("zsetValue-object:{}",zset);//bitmapredisUtils.setBit("bitmap",1,true);redisUtils.setBit("bitmap",0,true);boolean bit = redisUtils.getBit("bitmap",1);log.info("bitmapValue-boolean:{}",bit);long count = redisUtils.bitCount("bitmap");log.info("bitmapValue-count:{}",count);long start = 0;long end = 7;long count2 = redisUtils.bitCount("bitmap",start,end);log.info("bitmapValue-count2:{}",count2);List<Long> bitFiledLong = redisUtils.bitField("bitmap",2,0);log.info("bitmapValue-bitFiledLong:{}",bitFiledLong);// 在线用户String onlineKey = "online:";for (int i = 0; i < 100; i++) {redisUtils.setBit(onlineKey, i, i % 2 == 0);}for (int i = 0; i < 10; i++) {log.info("{}={}" ,i, redisUtils.getBit(onlineKey, i));}log.info("online:{}",redisUtils.bitCount(onlineKey));// IP去重复统计final String logLogKey = "loglog:";String[] arr = new String[100];for (int i = 0; i < 100;i++) {arr[i] = "A" + new Random().nextInt(10) + 1;}redisUtils.setHyperLogLog(logLogKey, arr);log.info("arr:{}",arr);log.info("logLog:{}",redisUtils.getHyperLogLog(logLogKey));BoundGeoOperations boundGeoOperations = redisUtils.setGeo("CHINA:CITY");Point nanjing = new Point(118.803805, 32.060168);boundGeoOperations.add(nanjing, "南京市");Point beijing = new Point(116.397039, 39.9077);boundGeoOperations.add(beijing, "北京市");Point shanghai = new Point(120.52, 30.40);boundGeoOperations.add(shanghai, "上海市");//geodist:获取两个地理位置的距离Distance distance = boundGeoOperations.distance("南京市","北京市", Metrics.KILOMETERS);log.info("南京市到北京市之间的距离是:{}km" ,distance.getValue());Distance distance2 = boundGeoOperations.distance("南京市", "上海市", Metrics.KILOMETERS);log.info("南京市到上海市之间的距离是:{}km", distance2.getValue());//geohash:获取某个地理位置的geohash值List<String> list2 = boundGeoOperations.hash("南京市");log.info("南京市的geoHash = {}", list2.get(0));//geopos:获取某个地理位置的坐标List<Point> pointList = boundGeoOperations.position("南京市");log.info("南京市的经纬度为 = {} ", pointList.get(0));//georadius:根据给定地理位置坐标获取指定范围内的地理位置集合// 查询南京市1000KM范围内的城市Circle within = new Circle(nanjing, 1000000);//设置geo查询参数RedisGeoCommands.GeoRadiusCommandArgs geoRadiusArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();//查询返回结果包括距离和坐标geoRadiusArgs = geoRadiusArgs.includeCoordinates().includeDistance();//按查询出的坐标距离中心坐标的距离进行排序geoRadiusArgs.sortAscending();//限制查询返回的数量geoRadiusArgs.limit(2);GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = boundGeoOperations.radius(within, geoRadiusArgs);List<GeoResult<RedisGeoCommands.GeoLocation<String>>> geoResultList = geoResults.getContent();for (GeoResult geoResult : geoResultList) {log.info("geoRadius {} " , geoResult.getContent());}//georadiusbymember:根据给定地理位置获取指定范围内的地理位置集合geoRadiusArgs.limit(1); geoResults = boundGeoOperations.radius("南京市", new Distance(1000000), geoRadiusArgs);geoResultList = geoResults.getContent();for (GeoResult geoResult : geoResultList) {log.info("geoRadiusByMember {}" , geoResult.getContent());} //删除位置信息,此命令不是geo提供的,是使用zrem命令删除的boundGeoOperations.remove("南京市");}
}

log

hash-object:User(account=210123, name=阿丹, age=25)
listValue-object:[a, b, a, b]
setValue-object:[{adana=User(account=210123, name=阿丹a, age=18), adan=User(account=210123, name=阿丹, age=25)}]
zsetValue-object:[18, 12, 123, 14]
bitmapValue-boolean:true
bitmapValue-count:2
bitmapValue-count2:2
bitmapValue-bitFiledLong:[3]
0=true
1=false
...online:50
arr:A61
logLog:10
南京市到北京市之间的距离是:899.2222km
南京市到上海市之间的距离是:246.4453km
南京市的geoHash = wtsqrkqk120
南京市的经纬度为 = Point [x=118.803805, y=32.060168]
geoRadius RedisGeoCommands.GeoLocation(name=南京市, point=Point [x=118.803805, y=32.060168])
geoRadius RedisGeoCommands.GeoLocation(name=上海市, point=Point [x=120.520001, y=30.400000])
geoRadiusByMember RedisGeoCommands.GeoLocation(name=南京市, point=Point [x=118.803805, y=32.060168])











虽然总结了,但是理解还不是很透彻!!!
菜了菜了!!!

参考:
https://www.sohu.com/a/300039010_114877
https://www.matools.com/blog/190329767
https://blog.csdn.net/weixin_39765695/article/details/110726589
https://www.jianshu.com/p/5596c3a4978d

Redis总结二 - 测试案例相关推荐

  1. 阿里云Redis性能压力测试(二十)

    文章目录 1.云Redis性能压力测试 2.安装redis-banchmark压测工具 3.压测两节点的Redis集群 4.压测四节点的Redis集群 4.1.扩容集群为四节点 4.2.压力测试 5. ...

  2. 敏态下“骨架化、模块化”测试案例编写技术实践

    文/叶婷婷 罗章坤 一.引言 随着互联网金融监管的日益严格.市场竞争的不断加剧及客户需求的快速变化,金融企业IT系统的复杂程度不断提高,IT需求日益放大,创新型需求持续产生.传统的软件开发模式,诸如瀑 ...

  3. 测试案例中@SpringBootTest与@RunWith**的含义

    背景:平常都是写功能,写业务代码忽略了对测试案例的理解,借此机会梳理记录一下测试案例中常用到的的几个注解. 一:@SpringBootTest 作用是加载ApplicationContext,启动sp ...

  4. Python自动化-APPium原理解析与实际测试案例分享

    目录结构 一.Appium概述 Appium架构原理 运行原理 1)Appium服务器 2)Bootstrap.jar 3)Appium客户端 二.Appium组件 三.Appium环境搭建 Node ...

  5. **Hadoop Ubuntu系统搭建攻略全详细!!!附带Hadoop搭建成功后测试案例**

    Hadoop Ubuntu系统搭建攻略全详细!!!附带Hadoop搭建成功后测试案例 Hadoop搭建教程 一.需要安装的工具: 1.首先确保虚拟机能连上网. 2.更新源列表: sudo apt-ge ...

  6. UI自动化工具Cypress测试案例、生成报告---windows版

    一.提前环境准备 node.js chrome浏览器 二.拉取gitLad上的测试案例代码 例如:拉取下来的文件夹是 -\xbox_test\cypress–0.4 如图: 三.进入拉取代码的根目录 ...

  7. UI自动化工具Cypress测试案例、生成报告---Linux版

    一.提前环境准备 node.js 必须 二.拉取gitLad上的测试案例代码 例如:拉取下来的文件夹是 -/xbox_test/cypress–0.4 如图:目录示范 三.进入拉取代码的根目录 例如: ...

  8. WebRTC系列<二> 案例与工具

     阅读关于webRTC的其他文章: WebRTC系列<一> 什么是WebRTC? WebRTC系列<二> 案例与工具 ----------------------------- ...

  9. Kafka吞吐量测试案例

    Kafka吞吐量测试案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 领英公司参考连接:https://www.slideshare.net/JiangjieQin/produc ...

最新文章

  1. 南阳汉诺塔 一 java_南阳明清移民记.pdf
  2. 开发工具总结(4)之Android Studio3.0填坑指南
  3. VMware虚拟产品简介
  4. twitter自定义api_为Twitter4j创建自定义SpringBoot Starter
  5. 基于小波包的图像压缩及matlab实现,基于小波包的图像压缩及matlab实现精选.doc...
  6. 完善区块链产业链 加速经济数字化转型
  7. pytest测试框架(六)---使用skip和skipif跳过测试用例
  8. C语言标准库函数 qsort 详解
  9. C和指针 第五章 习题
  10. wps 甘特图_【WPS神技能】在Excel表格中用图表阶梯式的展示任务进程?找甘特图呀...
  11. 约数之和(分治,公式变形)
  12. Joomla模板制作教程(转)
  13. CKEditor设置背景图片及宽高
  14. 大恒相机文件配置V1.1
  15. 在华为云ECS上部署openGauss
  16. screen Attached
  17. LDPC的信道编译码的matlab仿真——LDPC译码最小和和归一化最小和
  18. 某玩具商的数据库操作
  19. 主板知识详解:支持内存类型
  20. Webots平台NAO机器人寻路避障实现

热门文章

  1. C++多态的原理(虚函数指针和虚函数表)
  2. C++单个类的所有对象是否共享虚函数表的验证
  3. writely is cool!
  4. SpringBoot中业务层标准开发和快速开发(大全)
  5. nvm安装nodejs 运行use命令时报错exit status 1: ��û���㹻��Ȩ��ִ�д˲�����
  6. 51假期读书笔记(上)——流畅的python
  7. 做网站必须托管服务器吗,做网站必须托管服务器吗
  8. CPI即消费者物价指数
  9. 旧金山大学网站的红黑树演示动画,益于理解红黑树
  10. LoRa节点开发:4、代码详解 LoRaWAN节点入网