1.添加redisson依赖
1.1 redisson文档
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.5.7</version>
</dependency>
2.创建CacheManager使用redisson的配置文件类
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 2 * @Author: ZhangShuai
* 3 * @Date: 2020/6/24 10:19
* 4 Redisson提供了将Redis无缝整合到Spring框架的能力。
* Redisson依照Spring Cache标准提供了基于Redis的Spring缓存实现。
* 每个缓存(Cache)实例都提供了了两个重要的可配置参数:过期时间(ttl)和最长空闲时间(maxIdleTime),
* 如果这两个参数都未指定或值为0,那么实例管理的数据将永久保存。
*/
@Configuration
@ComponentScan
@EnableCaching
public class RedisConfigManager {
@Bean(destroyMethod = "shutdown")
RedissonClient redisson() throws IOException {
Config config = new Config();
//这里使用你的redis地址 以及redis的密码 和redis要存储的数据库
config.useSingleServer().setAddress("redis://127.0.0.1:6379").setPassword("123").setDatabase(0);
return Redisson.create(config);
}
@Bean
CacheManager cacheManager(RedissonClient redissonClient) {
Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
// 创建一个名称为"testMap"的缓存,过期时间ttl为24分钟,同时最长空闲时maxIdleTime为12分钟。
config.put("testMap", new CacheConfig(24 * 60 * 1000, 12 * 60 * 1000));
return new RedissonSpringCacheManager(redissonClient, config);
}
}
3.使用Cacheable等注解
@Cacheable(value="users", key="#id")
public User find(Integer id) {
return null;
}
4.使用SpringBoot连接redis
4.1 创建redis配置类,配置redis要连接的机器
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 2 * @Author: ZhangShuai
* 3 * @Date: 2020/6/17 10:11
* 4 创建redis配置类,配置redis要连接的机器
*/
@Configuration
public class RedisConfig {
@Bean
public RedissonClient redissonClient() {
//配置当前redis要连接的信息 redis://127.0.0.1:6379
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379").setPassword("123").setDatabase(0);
//config.useSingleServer().setAddress("redis://"+address+":"+port).setPassword(password).setDatabase(database);
return Redisson.create(config);
}
}
4.2 :创建redis中常用的方法
import org.redisson.api.RBucket;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.concurrent.TimeUnit;
/**
* 2 * @Author: ZhangShuai
* 3 * @Date: 2020/6/17 10:11
* 4 创建redis中常用的方法
*/
@Component
public class RedisMethod {
@Autowired
private RedissonClient redissonClient;
//获取一个 key
public String getString(String key) {
//RExpirable RExpirable.remainTimeToLive(), RExpirable.remainTimeToLiveAsync(), RExpirableReactive.remainTimeToLive();
RBucket<Object> result = this.redissonClient.getBucket(key);
if (StringUtils.isEmpty(result.get())) {
return null;
}
return result.get().toString();
}
//获取一个key的过期时间
public long getTime(String key) {
long result = redissonClient.getPermitExpirableSemaphore(key).remainTimeToLive();
;
return result;
}
//设置一个 key vlaue的值
public void setString(String key, Object value) {
RBucket<Object> result = this.redissonClient.getBucket(key);
if (!result.isExists()) {
result.set(value);
}
}
//设置key的过期时间 time为秒单位
public void setStringTime(String key, Object value, Long time) {
RBucket<Object> result = this.redissonClient.getBucket(key);
if (!result.isExists()) {
result.set(value, time, TimeUnit.SECONDS);
}
}
//判断这个key是否存在
public boolean hasString(String key) {
RBucket<Object> result = this.redissonClient.getBucket(key);
if (result.isExists()) {
return true;
} else {
return false;
}
}
//删除一个key
public boolean delString(String key) {
RBucket<Object> result = this.redissonClient.getBucket(key);
if (result.delete()) {
return true;
} else {
return false;
}
}
public long incr(String key, long delta) {
return this.redissonClient.getAtomicLong(key).addAndGet(delta);
}
// -----------------------------------------------------------------------
public void lock() {
RCountDownLatch countDown = redissonClient.getCountDownLatch("aa");
countDown.trySetCount(1);
try {
countDown.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
RCountDownLatch latch = redissonClient.getCountDownLatch("countDownLatchName");
latch.countDown();
RReadWriteLock rwlock = redissonClient.getReadWriteLock("lockName");
rwlock.readLock().lock();
rwlock.writeLock().lock();
rwlock.readLock().lock(10, TimeUnit.SECONDS);
rwlock.writeLock().lock(10, TimeUnit.SECONDS);
try {
boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS);
boolean res1 = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
5.使用定义的方法获取redis的值
注入 RedisMethod 使用里面的方法 传入相对的参数 即可