入门:
引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
配置文件:
spring:
redis:
database: 0
host: 139.107.71.246
port: 6379
password: # 密码(默认为空)
timeout: 6000 # 连接超时时长(毫秒)
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
加入 data redis 配置文件:
package io.sr.modules.biz.sys.conf; import java.net.UnknownHostException; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; /** * @Description: Redis配置文件 * @author 刘彦青 * @date 2018年3月1日 */ @Configuration public class RedisConfiguration { @Bean @ConditionalOnMissingBean(name="redisTemplate") public RedisTemplate<Object, Object> redisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException{ RedisTemplate<Object, Object> template = new RedisTemplate<Object,Object>(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException{ StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }
使用:
RedisTemplate中定义了对5种数据结构操作
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
public class RedisUtils { @Autowired private StringRedisTemplate template; public void setKey(String key,String value){ ValueOperations<String, String> ops = template.opsForValue(); ops.set(key,value); } public String getValue(String key){ ValueOperations<String, String> ops = this.template.opsForValue(); return ops.get(key); }
常用的一些操作:
template.opsForValue().set("key", "100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间 template.boundValueOps("key").increment(-1);//val做-1操作 template.opsForValue().get("key")//根据key获取缓存中的val template.boundValueOps("key").increment(1);//val +1 template.getExpire("key")//根据key获取过期时间 template.getExpire("key",TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位 template.delete("key");//根据key删除缓存 template.hasKey("546545");//检查key是否存在,返回boolean值 template.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合 template.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间 template.opsForSet().isMember("red_123", "1")//根据key查看集合中是否存在指定数据 template.opsForSet().members("red_123");//根据key获取set集合
常用API
Redis的String数据结构 (推荐使用StringRedisTemplate)
- set void set(K key, V value);
set void set(K key, V value, long timeout, TimeUnit unit)
- set void set(K key, V value, long offset);
该方法是用 value 参数覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始
- setIfAbsent Boolean setIfAbsent(K key, V value);
- multiSet void multiSet(Map<? extends K, ? extends V> m);
为多个键分别设置它们的值
- multiSetIfAbsent Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m);
为多个键分别设置它们的值,如果存在则返回false,不存在返回true
- get V get(Object key);
- getAndSet V getAndSet(K key, V value);
设置键的字符串值并返回其旧值
- multiGet List<V> multiGet(Collection<K> keys);
为多个键分别取出它们的值
- increment Long increment(K key, long delta);
自增 支持整数, 当key已经存在时,这个可以实现自增或自减, value值等于旧值+delta
- increment Double increment(K key, double delta);
也支持浮点数
- append Integer append(K key, String value);
如果key已经存在并且是一个字符串,则该命令将该值追加到字符串的末尾。如果键不存在,则它被创建并设置为空字符串,因此APPEND在这种特殊情况下将类似于SET。
- get String get(K key, long start, long end);
截取key所对应的value字符串
- size Long size(K key);
返回key所对应的value值得长度
- setBit Boolean setBit(K key, long offset, boolean value);
对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)
key键对应的值value对应的ascii码,在offset的位置(从左向右数)变为value
- getBit Boolean getBit(K key, long offset);
获取键对应值的ascii码的在offset处位值
redis 发布与订阅 : https://zhuanlan.zhihu.com/p/20948779
详细用法:https://www.jianshu.com/p/7bf5dc61ca06