# smart-cache **Repository Path**: Extremepyz/smart-cache ## Basic Information - **Project Name**: smart-cache - **Description**: 基于Ehcache和Redis实现的分布式二级缓存.简单适用,全局可控,除基本操作以外实现多机集群时一级缓存的监控,管理和抓取. - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 34 - **Created**: 2017-08-15 - **Last Updated**: 2021-06-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # smart-cache ## Introduce Java distributed second-level cache, which is achieved based on Ehcache2 and Redis, **can realize cache monitoring and fetching of multi-computer cluster at all levels besides basic operations.** - Synchronize the stand-alone cache to the Redis caching-system in a specific structure to achieve the unified management of the global cache. - Establish CacheManager and StoreLocation with codes dynamically to get rid of annoying ehcache.xml configuration files and prevent the cache from becoming out of sync when the service needs to be restarted or stop working. - Replace the native hash type of Redis with the self-maintained hash data structure to make the expiration time of each cache element controllable. - Adopt Redis.Topic mechanism to realize cache synchronization. - Provide cache management monitoring pages. - Implement the fetching function of multi-machine local cache. - Realize easy operation for its similarity to the API of Jedis. - Minimize the configuration and packaging, striving to be streamlined. ## 项目介绍 基于Ehcache2和Redis实现的Java分布式二级缓存.除基本操作以外实现**多机集群时各级缓存的监管和抓取**. - 将单机缓存以特定结构同步至Redis存储,实现全局缓存统一管理. - 编码方式动态创建CacheManager和StoreLocation.摆脱了烦人的ehcache.xml配置文件,同时避免服务重启或不可用时缓存未同步的问题. - 使用自身维护的hash数据结构,替换Redis原生的hash类型,实现每个缓存元素过期时间可控. - 使用Redis.Topic机制.实现缓存同步. - 提供缓存管理监控页面. - 实现多机本地缓存抓取功能. - 类似Jedis的API,使用简便. - 尽可能减少配置和封装,力求精简,够用即可. 开源地址: [github](https://github.com/yrain/smart-cache) [oschina](http://git.oschina.net/yrain/smart-cache) ## 使用方式 - cmd ```bash mvn clean install -Dmaven.test.skip=true ``` - pom.xml ```xml com.smart smart-cache ${VERSION} ``` - spring.xml ```xml ``` - 代码示例 ```java cacheTemplate.set(name, key, value);// 设置 cacheTemplate.get(name, key);// 获取 cacheTemplate.del(name, key);// 删除 cacheTemplate.rem(name);// 删除name下所有缓存 cacheTemplate.cls();// 清空所有缓存 cacheTemplate.fetch(name, key);// 抓取集群下所有单机一级缓存 cacheTemplate.ttl(name, key, level);// 获取缓存ttl cacheTemplate.names();// 获取所有缓存name cacheTemplate.keys(name);// 获取name下所有key cacheTemplate.size(name);// 获取name下缓存数量 cacheTemplate.values(name);// 获取name下缓存值 ``` 见:smart-cache/src/test/java/com/smart/cache/App.java ## 与Spring Cache联用 - spring.xml ```xml ``` ## 与[AutoLoadCache](https://github.com/qiujiayu/AutoLoadCache)联用 - pom.xml ```xml com.smart smart-cache-autoload ${VERSION} ``` - spring.xml ```xml ``` ## 缓存监控 - web.xml ```xml SmartCache com.smart.servlet.CacheAdminServlet username admin password admin SmartCache /smartcache/* ``` ## 项目演示 ### 监控页面 - 登录页面:http://localhost:8080/smartcache/login.html - 用户密码:admin/admin - 监控页面:http://localhost:8080/smartcache/admin.html - 页面截图: ![SmartCacheAdmin](./doc/cache.admin.png "SmartCacheAdmin") ### Showcase.AutoLoadCache - 演示页面:http://localhost:8080/view/autoload/user.html - 页面截图: ![SmartCacheDemo.Autoload](./doc/cache.demo.autoload.png "SmartCacheDemo.Autoload") ### Showcase.Spring Cache - 查询缓存:http://localhost:8080/showcase/springcache/user/find - 添加数据:http://localhost:8080/showcase/springcache/user/create 注意: 若修改了序列化方式,务必清空Redis上原缓存数据 ## 完整配置 ```xml ``` 见:smart-cache/src/test/java/spring.xml ## Redis集群配置 ```xml ``` ## Redis密码配置 ```xml ``` ## 建议咨询 QQ:20365124 email:yrain@live.cn