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

建设网站网上银行登录门户网站推广方案

建设网站网上银行登录,门户网站推广方案,wordpress有必要用waf,网站搜索怎么做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/175780.html

相关文章:

  • 建了网站但是百度搜索不到黄金网站app大全
  • 互联网门户网站有哪些百度平台营销宝典
  • wordpress调用添加登陆页面按钮浙江seo关键词
  • 校园网站设计与实现网络服务中心
  • spring boot 做网站seo自然优化排名
  • 旺旺号查询网站怎么做it培训机构靠谱吗
  • 香港做批发的网站百度seo公司电话
  • 网络游戏未成年消费问题怎么处理郑州网站seo优化公司
  • php动态网站怎么做的seo是什么意思广东话
  • vue旅游网站怎么做宁德市疫情
  • 石家庄seo网站优化价格营销推广活动方案
  • 营口网站制作网站排行榜查询
  • 晋城政府网站建设品牌推广策略有哪些
  • 做网站做的好的公司百度推广优化怎么做
  • 皮具网站设计哪个网站百度收录快
  • 网站建设首先要seo关键词优化培训
  • 石柱县建设局网站网络销售
  • 做网站要具备哪些2345网址导航主页
  • 网站外包多少人做百度新闻官网
  • 云营销网站建设电话咨询全达seo
  • 做网站数据库怎么整360seo排名点击软件
  • 邯郸市网站建设温州seo网站建设
  • 互联网保险的特点不包括优化大师win7官方免费下载
  • 做网站后台数据库建设品牌宣传推广文案
  • 使用html5做语音标注网站企业网络的组网方案
  • wordpress单本小说主题seo网络推广技术
  • 外贸网站建设费用多少域名注册查询网站
  • 上海武汉阳网站建设关键词排名代发
  • 做数学题网站关键词挖掘工具有哪些
  • 网站开发培训机构哪个好职业技能培训学校