博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot整合redis-lettuce
阅读量:4042 次
发布时间:2019-05-24

本文共 3117 字,大约阅读时间需要 10 分钟。

0、前言

redis客户端采用lettuce

1、先上代码目录

在这里插入图片描述

2、pom文件

1)父pom

com.alicp.jetcache
jetcache-starter-redis-lettuce
2.6.0

2)子模块pom

com.alicp.jetcache
jetcache-starter-redis-lettuce
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-web

3、application.yml

jetcache:  statIntervalMinutes: 1 #统计间隔  areaInCacheName: false  local:    default: #默认area      type: caffeine      keyConvertor: fastjson  remote:    default:      type: redis.lettuce #使用lettuce      keyConvertor: fastjson      valueEncoder: java      valueDecoder: java      poolConfig:        minIdle: 1        maxIdle: 50        maxTotal: 1000        maxWait: 1000      #     uri: ['redis://password@192.168.126.131:6379/0','redis://password@192.168.126.132:6379/0','redis://password@192.168.126.133:6379/0']      uri:        - redis://192.168.126.133:6379 #redis://密码@IP:端口/库

4、UserController

package com.ct.redis.lettuce.controller;import com.ct.redis.lettuce.entity.UserEntity;import com.ct.redis.lettuce.service.impl.UserServiceImpl;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/** * @Author LaoHa * @Date 2021/6/12 */@Slf4j@RestControllerpublic class UserController {    @Autowired    UserServiceImpl userService;    @GetMapping(value = "/getUser")    public String getUserInfo() {        userService.getUserNameCache().put("123", "中华人民共和国万岁!");        log.info("userNameCache : " + userService.getUserNameCache().get("123"));        UserEntity user = new UserEntity();        user.setName(userService.getUserNameCache().get("123"));        userService.getUserCache().put("1234", user);        log.info("userCache: " + userService.getUserCache().get("1234").getName());        return userService.getUserCache().get("1234").getName();    }}

5、UserService

package com.ct.redis.lettuce.service.impl;import com.alicp.jetcache.Cache;import com.alicp.jetcache.anno.CacheType;import com.alicp.jetcache.anno.CreateCache;import com.ct.redis.lettuce.entity.UserEntity;import lombok.Data;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;/** * @Author LaoHa * @Date 2021/6/12 */@Data@Slf4j@Servicepublic class UserServiceImpl {    /**     * 定义一个是String类型的远程缓存      */    @CreateCache(name ="ct.username", expire = 120, cacheType = CacheType.REMOTE)    private Cache
userNameCache; /** * 定义一个是User对象的二级缓存(本地+远程) */ @CreateCache(name ="ct.user", localExpire = 60, localLimit = 100, expire = 120, cacheType = CacheType.BOTH) private Cache
userCache;}

6、UserEntity

同《》

7、AppRedis

同《》

8、运行测试

运行AppRedis启动,浏览器输入:

http://localhost:8088/getUser

输出:

中华人名工程国

2)Redis Desktop Manager

立即查看,db0(3),过段时间(超过expire time),db(1)

转载地址:http://mlmdi.baihongyu.com/

你可能感兴趣的文章
Truncate 表之恢复
查看>>
Oracle DG failover 后恢复
查看>>
mysql 主从同步配置
查看>>
为什么很多程序员都选择跳槽?
查看>>
mongdb介绍
查看>>
mongdb安装使用
查看>>
mongdb在java中的应用
查看>>
区块链技术让Yotta企业云盘为行政事业服务助力
查看>>
yotta企业云盘助力制造行业创高峰
查看>>
Yotta企业云盘更好的为媒体广告业服务
查看>>
Yotta企业云盘助力旅游行业新发展
查看>>
Yotta企业云盘助力科技行业创高峰
查看>>
Yotta企业云盘更好地为教育行业服务
查看>>
Yotta企业云盘怎么帮助到能源化工行业
查看>>
企业云盘如何助力商业新发展
查看>>
医疗行业运用企业云盘可以带来什么样的提升
查看>>
教育数字智能化能为现有体系带来新的起点
查看>>
媒体广告业如何将内容资产进行高效地综合管理与利用
查看>>
能源化工要怎么管控核心数据
查看>>
媒体广告业如何运用云盘提升效率
查看>>