本文主要记录ehcache和redis实现缓存
一、ehcache
1.ehcache:用来管理Java中缓存的轻量级工具,其核心通过CacheManager使用,一般使用于单机版,可以部署集群,但是不推荐,一般使用redis或者其他框架来实现分布式缓存。
2.缓存过期策略:FIFO(先进先出原则)、LRU(最近最少使用,即目前最久未被使用的优先淘汰)、LFU(最近不常用[算法类似于JVM中垃圾回收器的s0和s1区],在一段时间中使用次数最少)
3.使用场景:一般用做一级缓存[单机版],redis做二级缓存[集群版]
4.示列代码(给出核心部分)
二、redis(单机版)
三、redis(主从复制)
四、redis(集群版)
======================================
缓存Map(ehcache)
1 @Component 2 public class EhcacheMap<K, V> { 3 4 private Map<K, V> echcache = new ConcurrentHashMap<>(); 5 6 public void put(K key, V value) { 7 echcache.put(key, value); 8 } 9 10 public V get(K key) { 11 return echcache.get(key); 12 } 13 14 public void remove(K key) { 15 echcache.remove(key); 16 } 17 }
ehcache.xml(ehcache)
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <diskStore path="java.io.tmpdir/ehcache-rmi-4000" /> <!-- 多台机器配置 rmiUrls=//192.168.8.32:400002/demoCache|//192.168.5.231:400003/demoCache --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual,rmiUrls=//127.0.0.1:5000/userCache"> </cacheManagerPeerProviderFactory> <!-- 配置 rmi 集群模式 --> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=127.0.0.1,port=4000,socketTimeoutMillis=120000" /> <!-- 多播方式配置 搜索某个网段上的缓存 timeToLive 0是限制在同一个服务器 1是限制在同一个子网 32是限制在同一个网站 64是限制在同一个region 128是限制在同一个大洲 255是不限制 <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=224.1.1.1, multicastGroupPort=40000, timeToLive=32" /> --> <!-- 默认缓存 --> <defaultCache maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="true" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache>
<!-- name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。
maxElementsInMemory:在内存中缓存的element的最大数目。
maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。
-->
<cache name="userCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" /> <!-- 用于在初始化缓存,以及自动设置 --> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache> </ehcache>
controller(ehcache)
@RestController public class EhcacheSingleVersionController { @Autowired private UserService userService; @Autowired private CacheManager cacheManager; @Autowired private EhcacheMap<String, String> ehcacheMap; @RequestMapping("/clear") public String clear() { cacheManager.getCache("userCache").clear(); return "success"; } @RequestMapping("/getUser") public List<User> getUser(Long id) { return userService.getUser(id); } @RequestMapping("/save") public String save(String name, Long id) { ehcacheMap.put(id.toString(), name); return "success"; } }
dao(ehcache)
@CacheConfig(cacheNames = "userCache") public interface UserMapper { @Select("SELECT ID ,NAME,AGE FROM user where id=#{id}") @Cacheable List<User> getUser(@Param("id") Long id); }
手动修改数据库值(ehcache)