Ignite使用计算机内存存储缓存数据,达到提升缓存读写性能的。但是计算机内存往往是有限的,我们必须合理管理Ignite对内存的使用。
Ignite可以使用JVM堆外内存和堆内内存。使用堆外内存基本上会对JVM垃圾回收造成影响,也不会对JVM中的其他进程数据造成影响。但是使用堆内内存性能更高。
一般来说,开发人员应该设置一个比较大的堆外缓存和一个小一些的堆内缓存,并配置合理的缓存失效策略。
堆外缓存,堆内缓存,缓存失效策略配置十分简单,直接看代码(使用Ignite 2.0版本)
package com.coshaho.learn.ignite.offheap; import javax.cache.expiry.CreatedExpiryPolicy; import javax.cache.expiry.Duration; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.Ignition; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.DataPageEvictionMode; import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.configuration.MemoryConfiguration; import org.apache.ignite.configuration.MemoryPolicyConfiguration; /** * * IgniteMemoryManager.java Create on 2017年6月15日 下午2:42:52 * * 类功能说明: ignite 2.0 缓存管理策略 * * Copyright: Copyright(c) 2013 * Company: COSHAHO * @Version 1.0 * @Author coshaho */ public class IgniteMemoryManager { public static void main(String[] args) { // 堆外缓存参数配置 MemoryPolicyConfiguration memPlc = new MemoryPolicyConfiguration(); memPlc.setName("10M_offheap_memory"); // 堆外缓存最小必须10M memPlc.setInitialSize(10 * 1024 * 1024); memPlc.setMaxSize(10 * 1024 * 1024); // 堆外缓存最久未被访问删除策略 memPlc.setPageEvictionMode(DataPageEvictionMode.RANDOM_LRU); MemoryConfiguration memCfg = new MemoryConfiguration(); memCfg.setMemoryPolicies(memPlc); IgniteConfiguration cfg=new IgniteConfiguration(); cfg.setMemoryConfiguration(memCfg); Ignite ignite =Ignition.start(cfg); // 缓存配置 CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<Integer, String>(); cacheCfg.setName("myCache"); // 使用堆外缓存 cacheCfg.setMemoryPolicyName("10M_offheap_memory"); // 堆内缓存是否开启 cacheCfg.setOnheapCacheEnabled(false); // 堆内缓存先进先出删除策略,参数1000表示堆内最多存储1000条记录 // cacheCfg.setEvictionPolicy(new FifoEvictionPolicy(1000)); // 设置缓存过期时间 cacheCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES)); IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cacheCfg); for(int i = 0; i < 10; i++) { StringBuffer str = new StringBuffer(); for(int j = 0; j < 10000; j++) { str.append("100+byte:abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); } cache.put(i, str.append(i).toString()); System.out.println("frist get cache" + i +": " + cache.get(i).substring(0, 8)); } for(int i = 0; i < 10; i++) { if(null == cache.get(i)) { System.out.println("second get cache" + i +": null"); } else { System.out.println("second get cache" + i +": " + cache.get(i).substring(0, 8)); } } } }
上述代码配置了一个10M的堆外缓存,并且关闭堆内缓存,配置的堆外缓存失效策略为最久未被访问删除策略,所以运行代码后,由于放入缓存的数据超过10M,所以会舍弃ID靠前的缓存,运行结果完全符合预期
[14:46:44] Ignite node started OK (id=dd2c004c) [14:46:44] Topology snapshot [ver=1, servers=1, clients=0, CPUs=4, heap=0.88GB] frist get cache0: 100+byte frist get cache1: 100+byte frist get cache2: 100+byte frist get cache3: 100+byte frist get cache4: 100+byte frist get cache5: 100+byte frist get cache6: 100+byte frist get cache7: 100+byte frist get cache8: 100+byte frist get cache9: 100+byte second get cache0: null second get cache1: null second get cache2: null second get cache3: null second get cache4: null second get cache5: 100+byte second get cache6: 100+byte second get cache7: 100+byte second get cache8: 100+byte second get cache9: 100+byte