团队对封装组件的代码范例

RedisCacheTest.java 3.8KB

    package com.ai.ipu.cache.redis; import junit.framework.TestCase; import com.ai.ipu.basic.log.ILogger; import com.ai.ipu.basic.log.IpuLoggerFactory; import com.ai.ipu.cache.CacheFactory; import com.ai.ipu.cache.ICache; import com.ai.ipu.cache.redis.context.IpuContextData; import com.ai.ipu.cache.redis.context.IpuContextData2; import com.alibaba.fastjson.JSON; public class RedisCacheTest extends TestCase{ private static final transient ILogger LOGGER = IpuLoggerFactory.createLogger(RedisCacheTest.class); private String cacheSingleName; private String cacheClusterName; private String cacheKey = "KEY"; private String cacheContent = "CONTENT"; private String strValue = "123456"; private int intValue = 123456; @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); cacheSingleName = "single"; cacheClusterName = "ssn"; } public void testRedisSingle() throws Exception { // TODO Auto-generated method stub ICache cache = CacheFactory.getCache(cacheSingleName); cache.put(cacheKey, intValue); // LOGGER.debug(Integer.toString((Integer)cache.get(cacheKey))); System.out.println("cacheKey"+Integer.toString((Integer)cache.get(cacheKey))); System.out.println("cacheKey"+intValue); assertEquals(intValue, cache.get(cacheKey)); // System.out.println(); } public void testRedisCluster() throws Exception { // TODO Auto-generated method stub ICache cache = CacheFactory.getCache(cacheClusterName); cache.put(cacheKey, intValue); LOGGER.debug(Integer.toString((Integer)cache.get(cacheKey))); System.out.println(Integer.toString((Integer)cache.get(cacheKey))); System.out.println("cacheKey"+intValue); assertEquals(intValue, cache.get(cacheKey)); } public void testObjectCache() throws Exception { // TODO Auto-generated method stub ICache cache = CacheFactory.getCache(cacheSingleName); IpuContextData contextData = new IpuContextData(); contextData.put(cacheKey, strValue); cache.put(cacheContent, contextData); contextData = (IpuContextData) cache.get(cacheContent); System.out.println("strValue"+strValue); //cache.get(cacheKey) 对应的是存放缓存的hash值,其他取到的均为值等于123456的值 System.out.println("cache中获取key值"+cache.get(cacheKey)); System.out.println("contextData类中获取“KEY”的值"+contextData.getData().get("KEY")); assertEquals(strValue,contextData.get(cacheKey)); System.out.println("================="); System.out.println(contextData.get("KEY")); assertEquals(contextData.getData().get("KEY"),strValue); System.out.println("++++++++++++++++"); assertEquals(strValue, cache.get(cacheKey)); System.out.println("-------------------"); } public void testObjectJsonCache() throws Exception { // TODO Auto-generated method stub ICache cache = CacheFactory.getCache(cacheSingleName); IpuContextData contextData = new IpuContextData(); contextData.put(cacheKey, strValue); cache.put(cacheContent, JSON.toJSONString((contextData))); String strContextData = cache.get(cacheContent).toString(); // LOGGER.debug("strContextData===" + strContextData); System.out.println("strContextData===" + strContextData); IpuContextData2 contextData2 = JSON.parseObject(strContextData, IpuContextData2.class); System.out.println("contextData2.isDirty()===" + contextData2.isDirty()); // LOGGER.debug("contextData2.isDirty()===" + contextData2.isDirty()); assertEquals(strValue, contextData2.get(cacheKey)); } }