springboot与redis使用(1)
来源:互联网 发布:海德椭圆机 知乎 编辑:程序博客网 时间:2024/04/28 20:58
springboot已经通过利用spring-boot-test-autoconfigure,很好地集成了redis,因此在调用redisTemplate时候,直接注入即可,不需要另外写配置类,除非有特殊的业务要求
工具类可以将对象保存到redis中,至于字符串,列表等操作,以后继续写
配置pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --></parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId></dependency>
redis工具类
import java.io.Serializable;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;/** * * @author liangxj * */@Componentpublic class RedisUtil { @Autowired private RedisTemplate redisTemplate; /** * 存入redis中的key,以便区分日后可能会有其他系统接入到redis服务器中 */ @Value("${redis.application.name}") private String redisKey; /** * redis开关 */ @Value("${redis.application.isOpenRedis}") private boolean isOpenRedis; public void setRedisKey(String redisKey) { this.redisKey = redisKey; } /** * 添加 * * @param key * key * @param doamin * 对象 * @param expire * 过期时间(单位:秒),传入 -1 时表示不设置过期时间 */ public void put(String key, Object doamin, long expire) { redisTemplate.opsForHash().put(redisKey, key, doamin); if (expire != -1) { redisTemplate.expire(redisKey, expire, TimeUnit.SECONDS); } } /** * 删除 * * @param key * 传入key的名称 */ public void remove(String key) { redisTemplate.opsForHash().delete(redisKey, key); } /** * 查询 * * @param key * 查询的key * @return */ public Object get(String key) { return redisTemplate.opsForHash().get(redisKey, key); } /** * 获取当前redis库下所有对象 * * @return */ public List<Object> getAll() { return redisTemplate.opsForHash().values(redisKey); } /** * 查询查询当前redis库下所有key * * @return */ public Set<String> getKeys() { return redisTemplate.opsForHash().keys(redisKey); } /** * 判断key是否存在redis中 * * @param key * 传入key的名称 * @return */ public boolean isKeyExists(String key) { return redisTemplate.opsForHash().hasKey(redisKey, key); } /** * 查询当前key下缓存数量 * * @return */ public long count() { return redisTemplate.opsForHash().size(redisKey); }}
redis 配置
# REDIS (RedisProperties)# Redis数据库索引(默认为0)spring.redis.database=0 # Redis服务器地址pring.redis.host=127.0.0.1# Redis服务器连接端口spring.redis.port=6379 # Redis服务器连接密码(默认为空)spring.redis.password=redis# 连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.pool.max-wait=-1 # 连接池中的最大空闲连接spring.redis.pool.max-idle=8 # 连接池中的最小空闲连接spring.redis.pool.min-idle=0 # 连接超时时间(毫秒)spring.redis.timeout=3000#spring.redis.sentinel.master=**#此处属于给redis配置主从关系,以便分布#spring.redis.sentinel.nodes= ***.***.***.***:26379,***.***.***.***:26380,***.***.***.***:26381redis.application.name=qianhaicredit-gateway
阅读全文
0 0
- springboot与redis使用(1)
- Springboot Redis分布式集群(1)- Linux下redis安装与使用
- Springboot Redis分布式集群(1)- Linux下redis安装与使用
- 转载 springBoot与redis
- SpringBoot--使用redis缓存(1)
- SpringBoot中使用redis
- springboot+redis使用
- SpringBoot系列-Redis使用
- Springboot 使用Redis注解
- SpringBoot--使用Redis缓存
- SpringBoot--使用Redis缓存
- java springboot与redis整合
- SpringBoot中Redis的使用
- SpringBoot系列—Redis使用
- Springboot+Redis 配置和使用
- 1 Springboot中使用redis,自动缓存、更新、删除
- springboot个人使用问题与心得(1)
- springboot与redis整合案例(上)
- vysor原理以及Android同屏方案
- 使用HttpSessionListener接口监听Session的创建和失效
- WPF操作SQLite
- LR项目中性能测试觉见指标
- ORA-29886: feature not supported for domain indexes
- springboot与redis使用(1)
- Git配置多账号登录不同项目
- MTK平台根据特定项目定义一个驱动中用到的宏方法
- 使用CallerMemberName简化InotifyPropertyChanged的实现
- 欢迎使用CSDN-markdown编辑器
- Eclipse常用配置
- 同步异步
- jsp两种申明变量的区别
- xcode模拟器目录