Memcached(三)、パッケージMemcachedとEhcache


本論文では、EhcacheとMemcachedを簡単にパッケージ化し、クライアントプログラムについてehcacheとmemcachedの違いを知る必要がなく、キャッシュを配置するProvider類だけで両者の間で切り替えができ、ProviderはSpring IoCを通してクラスを注入する.
cache.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="cacheProvider" class="org.frank1234.spring.memcached.EhcacheCacheProvider">
    </bean>
    <bean id="cacheManager" class="org.frank1234.spring.memcached.CacheManager">
        <property name = "cache">
            <ref bean="cacheProvider"></ref>
        </property>
    </bean>
</beans>
public interface ICache {
    public void put(String key, Object obj) throws Exception;
    public Object get(String key) throws Exception;
    public void remove(String key) throws Exception;
}


public class EhcacheCacheProvider implements ICache{
    private static CacheManager manager ;
    private static Cache ehcache;
    public EhcacheCacheProvider(){
        manager =  CacheManager.getInstance();
        ehcache = manager.getCache("helloworldCache");
    }


    @Override
    public void put(String key, Object obj) throws Exception {
        Element elemnt = new Element(key,obj);
        ehcache.put(elemnt);
    }

    @Override
    public Object get(String key) throws Exception {
        Element element = ehcache.get(key);
        return element.getObjectValue();
    }

    @Override
    public void remove(String key) throws Exception {
        ehcache.remove(key);
    }
}


public class MemcachedCacheProvider implements ICache {
    static {
        try {
            initPool();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private static MemCachedClient client;

    private static void initPool() throws Exception{
        Properties props = getProperties();
        String[] servers = ((String)props.get("servers")).split(",");
        SockIOPool pool = SockIOPool.getInstance();
        pool.setServers(servers);
        pool.setFailover(Boolean.valueOf((String)props.get("failover")));
        pool.setInitConn(Integer.parseInt((String)props.get("initConn")));
        pool.setMinConn(Integer.parseInt((String)props.get("minConn")));
        pool.setMaxConn(Integer.parseInt((String)props.get("maxConn")));
        pool.setMaintSleep(Integer.parseInt((String)props.get("maintSleep")));
        pool.setNagle(Boolean.valueOf((String)props.get("magle")));
        pool.setSocketTO(Integer.parseInt((String)props.get("socketTO")));
        pool.setAliveCheck(Boolean.valueOf((String)props.get("aliveCheck")));
        pool.initialize();
    }
    private static Properties getProperties() throws Exception{
        Properties props = new Properties();
        InputStream in = MemcachedCacheProvider.class.getResourceAsStream("/memcached.properties");
        props.load(in);
        return props;
    }
    public MemcachedCacheProvider(){
        client = new MemCachedClient();
    }

    @Override
    public void put(String key, Object obj) throws Exception {
        client.set(key.toString(),obj);

    }

    @Override
    public Object get(String key) throws Exception {
        return client.get(key);
    }

    @Override
    public void remove(String key) throws Exception {
        client.delete(key);
    }
}


public class CacheManager {
    private ICache cache;

    public void setCache(ICache cache) {
        this.cache = cache;
    }

    public ICache getCache() throws Exception{
        return cache;
    }
}

public class CacheMain {
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {

    }
    @Test
    public void testCache() throws Exception{
        ApplicationContext factory = new ClassPathXmlApplicationContext("cache.xml");
        CacheManager cacheManager = (CacheManager)factory.getBean("cacheManager");
        ICache cache = cacheManager.getCache();
        cache.put("key","value");
        System.out.println(cache.get("key"));
    }
}

ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
         monitoring="autodetect" dynamicConfig="true">
    <!--              ,    "   1.data"     ,            ,         Thread.sleep(60*1000)     -->
    <diskStore path="d:/ehcache"/>
    <!--       
     maxElementsInMemory :           ,        ,maxBytesLocalHeap      ,jdk            api,ehcache            。       cache.calculateInMemorySize(),        。
     eternal:true      ,   false,     true, timeToIdleSeconds timeToLiveSeconds    。
     overflowToDisk:true            maxElementsInMemory           ,
     timeToIdleSeconds:             ,        ,      。
     timeToLiveSeconds:          ,        ,      。      timeToIdleSeconds    。
     memoryStoreEvictionPolicy:   maxElementsInMemory   ,Ehcache              。     :LRU(      ,    )、FIFO(    )、LFU(      )。


     -->
    <defaultCache  maxElementsInMemory="1000" eternal="false"
                  overflowToDisk="true" timeToIdleSeconds="30" timeToLiveSeconds="60">
    </defaultCache>

    <cache name="helloworldCache"  maxElementsInMemory="1000"
           maxEntriesLocalDisk="10000" eternal="false" overflowToDisk="true"
           diskSpoolBufferSizeMB="20" timeToIdleSeconds="10" timeToLiveSeconds="20"
           memoryStoreEvictionPolicy="LFU"   />
</ehcache>
memcached.properties
servers=localhost:11211
failover=true
initConn=10
minConn=5
maxConn=250
maintSleep=30
nagle=false
socketTO=3000
aliveCheck=true