什么是redisson:
Redisson是架设在Redis基础上的一个Java驻内存数据网(In-Memory Data Grid , 是一个高级的分布式协调Redis客服端,能帮助用户在分布式环境中轻松实现一些Java的对象,Redisson、Jedis、Lettuce 是三个不同的操作 Redis 的客户端,Jedis、Lettuce 的 API 更侧重对 Reids 数据库的 CRUD(增删改查),而 Redisson API 侧重于分布式开发。
redisson 和 redis 之间的区分
1.概况对比区分
Jedis是Redis的java实现的客户端,其API提供了比较全面的Redis命令的支持,Redisson实现了分布式和可扩展的的java数据结构,和Jedis相比,功能较为简单,不支持字符串操作,不支持排序,事物,管道,分区等Redis特性。Redisson的宗旨是促进使用者对Redis的关注分离,从而让使用者能够将精力更集中的放在处理业务逻辑上。
2.可伸缩性区分
Jedis使用阻塞的I/O,且其方法调用都是同步的,程序流程要等到sockets处理完I/O才能执行,不支持异步,Jedis客户端实例不是线程安全的,所以需要通过连接池来使用Jedis。Redisson使用非阻塞的I/O和基于Netty框架的事件驱动的通信层,其方法调用时异步的。Redisson的API是线程安全的,所以操作单个Redisson连接来完成各种操作。
3.第三方框架整合区分
Redisson在Redis的基础上实现了java缓存标准规范;Redisson还提供了Spring Session回话管理器的实现。
支持Redis多种连接模式
单例模式连接
Config config = new Config();
config.useSingleServer()
// .setAddress("redis://127.0.0.1:6379"); // 本地连接
.setAddress(DeploymentConfigService.getDeployment().getRedisAddress())
// TODO
.setPassword(DeploymentConfigService.getDeployment().getRedisAccessKey());
redisson = Redisson.create(config);
集群模式
Config config = new Config();
config.useClusterServers()
.setScanInterval(2000) // cluster state scan interval in milliseconds
.addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001")
.addNodeAddress("redis://127.0.0.1:7002");
RedissonClient redisson = Redisson.create(config);
哨兵模式连接
Config config = new Config();
config.useSentinelServers()
.setMasterName("mymaster")
.addSentinelAddress("redis://127.0.0.1:26389", "redis://127.0.0.1:26379")
.addSentinelAddress("redis://127.0.0.1:26319");
RedissonClient redisson = Redisson.create(config);
主从模式连接
Config config = new Config();
config.useMasterSlaveServers()
.setMasterAddress("redis://127.0.0.1:6379")
.addSlaveAddress("redis://127.0.0.1:6389", "redis://127.0.0.1:6332", "redis://127.0.0.1:6419")
.addSlaveAddress("redis://127.0.0.1:6399");
RedissonClient redisson = Redisson.create(config);
分布式集合
RMap<String, SomeObject> map = redisson.getMap("anyMap");
RMapCache<String, SomeObject> map = redisson.getMapCache("anyMap");
RLocalCachedMap<String, Integer> map = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
Map操作get、put、flush缓存并设置过期时间
package com.loreal.dataapi.core.datacache;
import com.loreal.dataapi.core.config.entitysettings.CachePolicy;
import com.loreal.dataapi.core.servingconfig.DeploymentConfigService;
import org.apache.olingo.commons.api.format.ContentType;
import org.redisson.Redisson;
import org.redisson.api.*;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.CompositeCodec;
import org.redisson.codec.MarshallingCodec;
import org.redisson.config.Config;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class RedisCache{
private RedissonClient redisson;
private RMapCache<String, String> cachedMap;
private RLocalCachedMap<String, String> localCachedMap;
private static volatile RedisCache apiSideCache;
// 单例apiSideCache
public static RedisCache getInstance() throws IOException {
if (apiSideCache == null) {
synchronized (RedisCache.class) {
if (apiSideCache == null) {
apiSideCache = new ApiSideCache();
}
}
}
return apiSideCache;
}
public RedisCache() throws IOException {
Config config = new Config();
config.useSingleServer()
// .setAddress("redis://127.0.0.1:6379");
.setAddress(DeploymentConfigService.getDeployment().getRedisAddress())
// TODO
.setPassword(DeploymentConfigService.getDeployment().getRedisAccessKey());
redisson = Redisson.create(config);
/* 开启本地缓存 */
LocalCachedMapOptions<String, String> options = LocalCachedMapOptions.<String, String>defaults()
.cacheSize(100000)
.evictionPolicy(LocalCachedMapOptions.EvictionPolicy.LRU)
.timeToLive(7, TimeUnit.DAYS);
localCachedMap = redisson.getLocalCachedMap("apiSyncedMap",new CompositeCodec(StringCodec.INSTANCE,new MarshallingCodec()), options);
MapOptions<String, String> map = MapOptions.<String, String>defaults();
cachedMap = redisson.getMapCache("apiCacheMap",new CompositeCodec(StringCodec.INSTANCE,new MarshallingCodec()), map);
}
/**
* 放缓存
*
* @param resultString
* @param tenantId
* @param uriInfo
* @param contentType
* @param useSyncedCache
* @param
*/
public void putCache(String resultString, String tenantId, String uriInfo, ContentType contentType,
boolean useSyncedCache, int CacheDurtionSec, String entitySetName) {
String cacheKey = tenantId + ":" + entitySetName + ":"
+ (uriInfo + contentType.toContentTypeString()).hashCode();
if (useSyncedCache == true) {
localCachedMap.put(cacheKey, resultString);
}
if (useSyncedCache == false) {
// 设置过期时间
cachedMap.put(cacheKey, resultString, CacheDurtionSec, TimeUnit.SECONDS);
// cachedMap.put(groupKey, resultString, CacheDurtionSec, TimeUnit.SECONDS);
}
}
/**
* 获取缓存
*
* @param cachePolicy
* @param
* @param uriInfo
* @param contentType
* @param useSyncedCache
* @return
*/
public String getCache(CachePolicy cachePolicy, String tenantId, String uriInfo, String contentType,
boolean useSyncedCache, String entitySetName) {
String cacheKey = String.valueOf(tenantId) + ":" + entitySetName + ":"
+ (uriInfo + contentType).hashCode();
if (useSyncedCache == true) {
// 判断是否开启本地缓存
String localCachedKey = localCachedMap.get(cacheKey);
return localCachedKey;
}
if (useSyncedCache == false) {
String key = cachedMap.get(cacheKey);
return key;
}
return null;
}
/**
* remove redis cache
* @param tenantId
* @param entitySetName
* @param useSyncedCache
* @throws IOException
*/
public void flushCache(String tenantId, String entitySetName, boolean useSyncedCache) throws IOException {
var keyPattern = tenantId + ":" + entitySetName + ":*";
if (useSyncedCache == true) {
var keySet = localCachedMap.keySet(keyPattern);
for (var key : keySet) {
localCachedMap.removeAsync(key);
}
}
if (useSyncedCache == false) {
var keySet =cachedMap.keySet(keyPattern);
for (var key : keySet) {
cachedMap.removeAsync(key);
}
}
}
}
版权声明:本文为qq_42864299原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。