当前位置: 首页 > wzjs >正文

建设网站网上银行登录上海牛巨微seo

建设网站网上银行登录,上海牛巨微seo,前端做网站需要,网站建设专业名词解释网站Redis 的 Spring 客户端使用 前面使用 Jedis 时, 是借助 Jedis 对象中的各种方法来对 Redis 进行操作. 而在 Spring 框架中, 则是通过 StringRedisTemplate 来操作 Redis. 最开始提供的类是 RedisTemplate, StringRedisTemplate 是 RedisTemplate 的子类, 专门用于处理文本数据…

Redis 的 Spring 客户端使用

前面使用 Jedis 时, 是借助 Jedis 对象中的各种方法来对 Redis 进行操作. 而在 Spring 框架中, 则是通过 StringRedisTemplate 来操作 Redis. 最开始提供的类是 RedisTemplate, StringRedisTemplate 是 RedisTemplate 的子类, 专门用于处理文本数据.

0. 配置 Spring 的 Redis环境

(1) 引入 Redis 的 Spring 依赖

选中 NoSQL 中的 Spring Data Redis (Access+Driver) 依赖.

image-20250305173733774

(2) 写 Spring 配置文件

application.yml:

spring:data:redis:host: 127.0.0.1port: 8888
(3) 创建 Controller 类, 并注入 StringRedisTemplate 对象.
@RestController
public class MyController { @AutowiredStringRedisTemplate stringRedisTemplate;
}

[!NOTE]

这里的 RedisTemplate 将 Redis 中的命令, 又做了进一步封装, 分成了几个类别 (每个类别操作特定的数据类型)

  • opsForValue(): 专门操作 string 类型.
  • opsForList(): 专门操作 list 类型.
  • opsForSet(): 专门操作 set 类型.
  • opsForHash(): 专门操作 hash 类型.
  • opsForZSet(): 专门操作 zset 类型.

这样一来, 它提供的一些接口风格和原生的Redis命令就存在一定差异.

还有一点要注意的是: Spring 并没有封装 Redis 的所有命令 (如 flushAll 就没有封装), 此时我们可以使用 execute 方法来使用 Redis 的原始命令.

例如:

stringRedisTemplate.execute((RedisConnection connection) -> {// execute 要求回调方法中必须写 return 语句,返回个东西// 这个回调返回的对象,就会作为 execute 本身的返回值connection.flushAll();return null;});
//这里的RedisConnection对象, 就相当于Jedis里的Jedis对象.

1.使用 string

@RestController
public class MyController {@AutowiredStringRedisTemplate stringRedisTemplate;@GetMapping("/testString")public String testString() {stringRedisTemplate.opsForValue().set("key", "111");stringRedisTemplate.opsForValue().set("key2", "222");stringRedisTemplate.opsForValue().set("key3", "333");String value = stringRedisTemplate.opsForValue().get("key");System.out.println("value: " + value);return "OK";}
}
  • 请求结果:

postman:

image-20250305182121278

日志:

image-20250305182205671

2. 使用 list

    @GetMapping("/testList")public String testList() {// 先清除之前的数据tringRedisTemplate.execute((RedisConnection connection) -> {// execute 要求回调方法中必须写 return 语句,返回个东西// 这个回调返回的对象,就会作为 execute 本身的返回值connection.flushAll();return null;});stringRedisTemplate.opsForList().leftPush("key", "111");stringRedisTemplate.opsForList().leftPush("key", "222");stringRedisTemplate.opsForList().leftPush("key", "333");String value = stringRedisTemplate.opsForList().rightPop("key");System.out.println("value: " + value);value = stringRedisTemplate.opsForList().rightPop("key");System.out.println("value: " + value);value = stringRedisTemplate.opsForList().rightPop("key");System.out.println("value: " + value);return "OK";}
  • 运行结果:

image-20250305195044857

3. 使用 set

    @GetMapping("/testSet")public String testSet() {stringRedisTemplate.execute((RedisConnection connection) -> {connection.flushAll();return null;});stringRedisTemplate.opsForSet().add("key", "111", "222", "333");Set<String> result = stringRedisTemplate.opsForSet().members("key");System.out.println("result: " + result);Boolean exists = stringRedisTemplate.opsForSet().isMember("key", "111");System.out.println("exists: " + exists);Long count = stringRedisTemplate.opsForSet().size("key");System.out.println("count: " + count);stringRedisTemplate.opsForSet().remove("key", "111", "222");result = stringRedisTemplate.opsForSet().members("key");System.out.println("result: " + result);return "OK";}
  • 运行结果:

image-20250305195721095

4. 使用 Hash

    @GetMapping("/testHash")public String testHash() {stringRedisTemplate.execute((RedisConnection connection) -> {connection.flushAll();return null;});stringRedisTemplate.opsForHash().put("key", "f1", "111");stringRedisTemplate.opsForHash().put("key", "f2", "222");stringRedisTemplate.opsForHash().put("key", "f3", "333");String value = (String) stringRedisTemplate.opsForHash().get("key", "f1");System.out.println("value: " + value);Boolean exists = stringRedisTemplate.opsForHash().hasKey("key", "f1");System.out.println("exists: " + exists);stringRedisTemplate.opsForHash().delete("key", "f1", "f2");Long size = stringRedisTemplate.opsForHash().size("key");System.out.println("size: " + size);return "OK";}
  • 运行结果:

image-20250305201837017

5. 使用 zset

    @GetMapping("/testZSet")public String testZSet() {stringRedisTemplate.execute((RedisConnection connection) -> {connection.flushAll();return null;});stringRedisTemplate.opsForZSet().add("key", "zhangsan", 10D);stringRedisTemplate.opsForZSet().add("key", "lisi", 20D);stringRedisTemplate.opsForZSet().add("key", "wangwu", 30D);Set<String> members = stringRedisTemplate.opsForZSet().range("key", 0, -1);System.out.println("members: " + members);Set<ZSetOperations.TypedTuple<String>> membersWithScore = stringRedisTemplate.opsForZSet().rangeWithScores("key", 0, -1);System.out.println("membersWithScore: " + membersWithScore);Double score = stringRedisTemplate.opsForZSet().score("key", "zhangsan");System.out.println("score: " + score);stringRedisTemplate.opsForZSet().remove("key", "zhangsan");Long size = stringRedisTemplate.opsForZSet().size("key");System.out.println("size: " + size);Long rank = stringRedisTemplate.opsForZSet().rank("key", "lisi");System.out.println("rank: " + rank);return "OK";}
  • 运行结果:

image-20250305202913409

http://www.dtcms.com/wzjs/237431.html

相关文章:

  • 虚拟网站建设免费网站谁有靠谱的
  • 阿里巴巴国际站怎么注册东台网络推广
  • ims2009 asp企业网站建设百度搜索风云榜电视剧
  • 衡阳市建设学校官方网站青岛网站建设有限公司
  • 做好网站开发工作总结网络seo是什么工作
  • 下载用的网站怎么做推广方案有哪些
  • 专门做美妆的网站百度竞价托管靠谱吗
  • 四川网站建设一站式服务商百度推广手机客户端
  • 有知道做网站的吗策划网络营销方案
  • 网站全站开发需要学什么凡科建站的免费使用
  • 网站建设开发案例百度收录软件
  • 工商代办公司windows优化大师官方网站
  • 广州有几个区几个县级市抚州seo外包
  • 开私服传奇做网站需要钱嘛seo优化排名是什么
  • wap手机网站代码全网营销有哪些平台
  • 企业网站的基本内容以及营销功能360收录
  • 大学英文网站建设开发一个网站需要多少钱
  • 建立电商网站什么软件推广效果好
  • 如何破解网站后台账号和密码免费建站工具
  • 万虹点读机如何做系统下载网站市场营销最有效的手段
  • 网站建设与排名百度竞价排名费用
  • 重庆网站seo多少钱百度软件优化排名
  • 网站域名hk百度指数关键词未收录怎么办
  • 真正做新闻网站百度推广登录入口官网网
  • wordpress做网站卡吗班级优化大师
  • 自己建网站需要备案吗明天上海封控16个区
  • 百度不收录我的网站深圳关键词排名推广
  • 黄冈网站推广在线观看百度一下网页版
  • 长春平原网站建设seo引擎优化
  • 吴江做网站的公司seo也成搜索引擎优化