Redis
Jedis,Redisson,Lettuce
-
1.1、Jedis
是 Redis 的 Java 实现的客户端。支持基本的数据类型如:String、Hash、List、Set、Sorted Set。
特点:使用阻塞的 I/O,方法调用同步,程序流需要等到 socket 处理完 I/O 才能执行,不支持异步操作。Jedis 客户端实例不是线程安全的,需要通过连接池来使用 Jedis。
-
1.2、Redisson
优点点:分布式锁,分布式集合,可通过 Redis 支持延迟队列
-
1.3、 Lettuce
用于线程安全同步,异步和响应使用,支持集群,Sentinel,管道和编码器。
基于 Netty 框架的事件驱动的通信层,其方法调用是异步的
RedisTemplate《=========================》Jedis
application-dev.yml
spring:
redis:
host: 192.168.1.140
port: 6379
password:
database: 15 # 指定redis的分库(共16个0到15)
---------------
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public CustomersEntity findById(Integer id) {
// 需要缓存
// 所有涉及的缓存都需要删除,或者更新
try {
String toString = stringRedisTemplate.opsForHash().get(REDIS_CUSTOMERS_ONE, id + "").toString();
if (toString != null) {
return JSONUtil.toBean(toString, CustomersEntity.class);
}
} catch (Exception e) {
e.printStackTrace();
}
// 缓存为空的时候,先查,然后缓存redis
Optional<CustomersEntity> byId = customerRepo.findById(id);
if (byId.isPresent()) {
CustomersEntity customersEntity = byId.get();
try {
stringRedisTemplate.opsForHash().put(REDIS_CUSTOMERS_ONE, id + "", JSONUtil.toJsonStr(customersEntity));
} catch (Exception e) {
e.printStackTrace();
}
return customersEntity;
}
return null;
}
RedissonClient 《===========================》Redisson
添加配置文件 Yaml 或者 json 格式
redisson-config.yml
# Redisson 配置
singleServerConfig:
address: "redis://192.168.1.140:6379"
password: null
clientName: null
database: 15 #选择使用哪个数据库0~15
idleConnectionTimeout: 10000
pingTimeout: 1000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
reconnectionTimeout: 3000
failedAttempts: 3
subscriptionsPerConnection: 5
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
connectionMinimumIdleSize: 32
connectionPoolSize: 64
dnsMonitoringInterval: 5000
#dnsMonitoring: false
threads: 0
nettyThreads: 0
codec:
class: "org.redisson.codec.JsonJacksonCodec"
transportMode: "NIO"
或者,配置 redisson-config.json
{
"singleServerConfig": {
"idleConnectionTimeout": 10000,
"pingTimeout": 1000,
"connectTimeout": 10000,
"timeout": 3000,
"retryAttempts": 3,
"retryInterval": 1500,
"reconnectionTimeout": 3000,
"failedAttempts": 3,
"password": null,
"subscriptionsPerConnection": 5,
"clientName": null,
"address": "redis://192.168.1.140:6379",
"subscriptionConnectionMinimumIdleSize": 1,
"subscriptionConnectionPoolSize": 50,
"connectionMinimumIdleSize": 10,
"connectionPoolSize": 64,
"database": 0,
"dnsMonitoring": false,
"dnsMonitoringInterval": 5000
},
"threads": 0,
"nettyThreads": 0,
"codec": null,
"useLinuxNativeEpoll": false
}
读取配置
新建读取配置类
@Configuration
public class RedissonConfig {
@Bean
public RedissonClient redisson() throws IOException {
// 两种读取方式,Config.fromYAML 和 Config.fromJSON
// Config config = Config.fromJSON(RedissonConfig.class.getClassLoader().getResource("redisson-config.json"));
Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson-config.yml"));
return Redisson.create(config);
}
}
@RestController
@RequestMapping("/")
public class TeController {
@Autowired
private RedissonClient redissonClient;
static long i = 20;
static long sum = 300;
// ========================== String =======================
@GetMapping("/set/{key}")
public String s1(@PathVariable String key) {
// 设置字符串
RBucket<String> keyObj = redissonClient.getBucket(key);
keyObj.set(key + "1-v1");
return key;
}
@GetMapping("/get/{key}")
public String g1(@PathVariable String key) {
// 设置字符串
RBucket<String> keyObj = redissonClient.getBucket(key);
String s = keyObj.get();
return s;
}
// ========================== hash =======================-=
@GetMapping("/hset/{key}")
public String h1(@PathVariable String key) {
Ur ur = new Ur();
ur.setId(MathUtil.randomLong(1,20));
ur.setName(key);
// 存放 Hash
RMap<String, Ur> ss = redissonClient.getMap("UR");
ss.put(ur.getId().toString(), ur);
return ur.toString();
}
@GetMapping("/hget/{id}")
public String h2(@PathVariable String id) {
// hash 查询
RMap<String, Ur> ss = redissonClient.getMap("UR");
Ur ur = ss.get(id);
return ur.toString();
}
// 查询所有的 keys
@GetMapping("/all")
public String all(){
RKeys keys = redissonClient.getKeys();
Iterable<String> keys1 = keys.getKeys();
keys1.forEach(System.out::println);
return keys.toString();
}
// ================== ==============读写锁测试 =============================
@GetMapping("/rw/set/{key}")
public void rw_set(){
// RedissonLock.
RBucket<String> ls_count = redissonClient.getBucket("LS_COUNT");
ls_count.set("300",360000000l, TimeUnit.SECONDS);
}
// 减法运算
@GetMapping("/jf")
public void jf(){
String key = "S_COUNT";
// RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
// atomicLong.set(sum);
// long l = atomicLong.decrementAndGet();
// System.out.println(l);
RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
if (!atomicLong.isExists()) {
atomicLong.set(300l);
}
while (i == 0) {
if (atomicLong.get() > 0) {
long l = atomicLong.getAndDecrement();
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
i --;
System.out.println(Thread.currentThread().getName() + "->" + i + "->" + l);
}
}
}
@GetMapping("/rw/get")
public String rw_get(){
String key = "S_COUNT";
Runnable r = new Runnable() {
@Override
public void run() {
RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
if (!atomicLong.isExists()) {
atomicLong.set(300l);
}
if (atomicLong.get() > 0) {
long l = atomicLong.getAndDecrement();
i --;
System.out.println(Thread.currentThread().getName() + "->" + i + "->" + l);
}
}
};
while (i != 0) {
new Thread(r).start();
// new Thread(r).run();
// new Thread(r).run();
// new Thread(r).run();
// new Thread(r).run();
}
RBucket<String> bucket = redissonClient.getBucket(key);
String s = bucket.get();
System.out.println("================线程已结束================================" + s);
return s;
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战