ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

springboot整合redis-lettuce

2021-06-12 22:33:02  阅读:328  来源: 互联网

标签:springboot jetcache redis lettuce import com ct


0、前言

redis客户端采用lettuce

1、先上代码目录

在这里插入图片描述

2、pom文件

1)父pom

            <dependency>
                <groupId>com.alicp.jetcache</groupId>
                <artifactId>jetcache-starter-redis-lettuce</artifactId>
                <version>2.6.0</version>
            </dependency>

2)子模块pom

     <dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-starter-redis-lettuce</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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
@RestController
public 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
@Service
public class UserServiceImpl {
    /**
     * 定义一个是String类型的远程缓存
      */
    @CreateCache(name ="ct.username", expire = 120, cacheType = CacheType.REMOTE)
    private Cache<String, String> userNameCache;
    /**
     * 定义一个是User对象的二级缓存(本地+远程)
     */
    @CreateCache(name ="ct.user", localExpire = 60, localLimit = 100, expire = 120, cacheType = CacheType.BOTH)
    private Cache<String, UserEntity> userCache;

}

6、UserEntity

同《springboot整合redis

7、AppRedis

同《springboot整合redis

8、运行测试

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

http://localhost:8088/getUser

输出:

中华人名工程国

标签:springboot,jetcache,redis,lettuce,import,com,ct
来源: https://blog.csdn.net/selfharding/article/details/117856371

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有