# redis-cache **Repository Path**: secondriver/redis-cache ## Basic Information - **Project Name**: redis-cache - **Description**: Spring-data-redis already realize Cache with Redis server, but redis-cache is another . - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 4 - **Created**: 2015-07-27 - **Last Updated**: 2022-09-21 ## Categories & Tags **Categories**: cache-modules **Tags**: None ## README ###Redis-Cache 一个简单的基于Redis服务的缓存库(由于缘故Redis,故可以实现分布式缓存) ####1. Redis-Cache提供的缓存模式: Redis-Cache使用jedis客户端来访问redis服务,提供了2种模式来进行数据缓存。 + Normal正常模式,单redis实例 + Sentinel模式,采用redis提供的Sentinel服务来进行主从复制和读写分离,不用关注redis实例数,只需要与Sentinel服务进行通信 > 注:如果要使用完备的Redis功能,则可以选择Spring-data-redis框架。 ####2. Redis-Cache key-value策略: Redis-Cache仅依赖jedis客户端,对缓存内容的key统一采用String类型,key的生成提供了自定义的方式;值进行序列化也提供了自定义方式。 + 默认key生成方式由`[cacheName][:][key]`即缓存名称+冒号+缓存内容关联的key;接口类:`secondriver.redis.cache.key.CacheKeyGenerator` + 默认value序列化方式采用fastjson进行序列化;接口类:`secondriver.redis.cache.serializer.CacheValueSerializer` ####3. Redis-Cache提供的Cache接口 每个Cache实体标识一类缓存项(即key-value对),Cache接口`secondriver.redis.cache.Cache`提供了添加缓存内容项,获取缓存内容项,删除缓存内容 项和清空Cache实体中所有缓存项(注:Cluster模式下暂不支持) Cache接口有如下具体实现: + Normal模式下:`secondriver.redis.cache.impl.JedisCache/RedisCache` + Sentinel模式下:`secondriver.redis.cache.impl.JedisCache/RedisCache` >注:这两种模式下提供统一实现,单依赖不同的`secondriver.redis.cache.core.JedisSupply/RedisSupply`接口实现 ####4. Redis-Cache的CacheManager接口 CacheManger接口实现主要用来集中管理用户创建了Cache实体,每个Cahe实体具有全局唯一的名称。 CacheManger接口实现:`secondriver.redis.cache.impl.RedisCacheManager` ####5. Redis服务数据源 Redis服务数据源是指连接到Redis服务器的连接管理对象,该对象由`secondriver.redis.cache.core.RedisDataSource`工具类担任,它提供了生成 三种模式下的Redis服务连接池对象,生成每个模式下的连接池对象遇到提供不同的配置,具体配置参见6.Redis-Cache配置 ####6. Redis-Cache配置 ``` #common password= #value is [0-(databases-1)), databases from redis server config database=0 timeout=1500 #If Normal mode, but have multi address please use * as prefix in using address address=192.168.88.153:26379;192.168.88.153:26380;192.168.88.153:26382 #Normal clientName=myclient #Sentinel masterName=mymaster #pool pool.maxTotal=8 pool.maxIdle=8 pool.minIdle=1 ``` Redis-Cache配置由`secondriver.redis.cache.confg.Configure`类以及2种模式下不同的子类配置类解析 ####7. 简单应用 使用6中的配置内容,采用Sentinel模式,代码示例: ```java //Step1: 读取配置文件 Properties properties = Configure.readInputStream(ClassLoader.getSystemResourceAsStream("redis.properties")); //Step2: 根据配置文件生成特定模式下的配置对象 SentinelConfigure configure = new SentinelConfigure(properties); //Step3: 根据指定模式下的配置对象生成Redis连接池或调度器 //全局唯一 JedisSupply supply = RedisDataSource.initialJedisSentinelPoolSupply(configure); //等价于:= new JedisSentinelPoolSupply(configure); //Step4: 创建Cache管理对象 CacheManager cacheManager = new RedisCacheManager(); //Step5: 创建指定模式下的缓存对象 Cache cache = new JedisCache("demo", supply); //Step6: 添加Cache对象到Cache管理对象 cacheManager.addCache(cache); //Step7: 获取Cache对象 Cache use = cacheManager.getCache("demo"); //Step8: 缓存数据 use.put("a1", "a1"); use.put("a2", "a2", 10000); Optional a1 = use.get("a1"); a1.ifPresent(o->{ System.out.println("a1=" + o); }); Optional a2 = use.get("a2");//有可能为null a2.ifPresent(o -> { System.out.println("a2=" + o); }); use.evict("a1"); use.clear(); //Step9:移除Cache对象 cacheManager.removeCache(cache); //Step10: 关闭Redis连接池或调度器 supply.closeJedisSupply(); ``` 使用RedisCache的方式与上诉略有不同: ```java //Step3: 根据指定模式下的配置对象生成Redis连接池或调度器 //全局唯一 RedisSupply supply = new RedisSentinelPoolSupply(configure); //Step5: 创建指定模式下的缓存对象 Cache cache = new RedisCache("demo", supply); //Step10: 关闭Redis连接池或调度器 supply.closeRedisSupply(); ``` ####8. 说明 该部分是使用jdk8构建,提供了Normal和Sentinel两种使用Redis服务构建缓存的方式.