Ignite緩存大小管理


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM