博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot + spring-data-redis
阅读量:7114 次
发布时间:2019-06-28

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

Redis

Redis是缓存, 消息队列, 多种类型的key-value存储服务.

Spring Boot

Spring Boot为Lettcue和Jedis客户端提供自动注入配置, 并且通过spring-data-redis提供抽象接口

配置连接Redis服务和接口调用

1. 加入依赖

pom.xml 的依赖集合中加入 org.springframework.boot:spring-boot-starter-data-reids 依赖, 如下配置

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis

默认使用 Lettuce 作为客户端

2. 修改配置文件

在spring boot配置文件中增加redis相关的配置, 以 application.yaml 为例 (其他格式配置文件,自行转换)

spring:  redis:    # 其他配置信息有缺省    host: localhost    port: 6379    timeout: 500    pool:      min-idle: 1      max-idle: 8      max-active: 8

3. Bean注入使用

如上配置完成之后, Spring Boot 自动注入管理 RedisTemplate. 可以通过该对象操作Redis.

按照我以往的简洁的做法, 我 在RedisTemple 上在封装成简洁明了的操作. 如下管理

RedisManager.java

package info.chiwm.boot.manager;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;/** * @author chiwm@kuyumall.com * @ClassName: RedisManager * @Description: * @date 2018/1/10 下午3:40 */@Componentpublic class RedisManager {    @Autowired    private StringRedisTemplate redisTemplate;    private static RedisManager redisManager;    @PostConstruct    public void init() {        redisManager = this;    }    /**     * Redis Set String Ops     *     * @param key     * @param value     */    public static void set(String key, String value) {        redisManager.redisTemplate.opsForValue().set(key, value);    }    /**     * Redis Get String Ops     * @param key     * @return     */    public static String get(String key) {        return redisManager.redisTemplate.opsForValue().get(key);    }}

直接调用静态方法的方式, 方便的调用Redis对应的set key命令. 如果还需其他存储类型和操作. 可以在 RedisManager 上增加静态方法.

转载于:https://blog.51cto.com/11931236/2059500

你可能感兴趣的文章
查看 MySQL 数据库中每个表占用的空间大小
查看>>
Linux登陆图形,佛祖保佑
查看>>
海洋迅雷VIP帐号获取器
查看>>
强制活动目录的站点复制
查看>>
在图形中添加一个图例
查看>>
os 模块 python file 与文件路径
查看>>
nginx 跳转配置
查看>>
VLAN概述
查看>>
我的友情链接
查看>>
部署Cacti监控平台
查看>>
我的友情链接
查看>>
HDU - 2041 - 超级楼梯(dp)
查看>>
α冲刺 (7/10)
查看>>
Autoit 自动化安装软件
查看>>
shell 脚本-----循环数组
查看>>
IBM x系列255服务器系统带宽特性
查看>>
8.23 课程-1 (history Tabb补全 别名 文件名展开{})
查看>>
重写equals方法
查看>>
Merge into 详细介绍
查看>>
MySQL Server参数优化 - innodb_file_per_table(独立表空间)
查看>>