栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过SpringCache缓存嵌套的可缓存操作

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过SpringCache缓存嵌套的可缓存操作

问题在于您是

methodA
直接从中访问的
methodB
,因此这将阻止通过处理缓存机制的Java代理。此外,您没有添加
@EnableCaching
注释,因此测试中实际上根本没有缓存。

以下测试表明,如果您正确地检查了Spring创建的代理,则嵌套缓存模式将按预期工作:

import static org.junit.Assert.*;import java.util.Arrays;import java.util.Random;import java.util.concurrent.atomic.AtomicInteger;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.Cacheable;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.concurrent.ConcurrentMapCache;import org.springframework.cache.support.SimpleCacheManager;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.test.annotation.DirtiesContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = { SpringCacheTest.Config.class })@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)public class SpringCacheTest {    private final static String CACHE_NAME = "testCache";    private final static AtomicInteger methodInvocations = new AtomicInteger(0);    public interface ICacheableService {        String methodA(int length);        String methodB(String name);    }    @Resource    private ICacheableService cache;    @Test    public void testNestedCaching() {        String name = "test";        cache.methodB(name);        assertEquals(methodInvocations.get(), 2);        cache.methodA(name.length());        // should only be 2 as methodA for this length was already invoked before        assertEquals(methodInvocations.get(), 2);    }    @Configuration    @EnableCaching    public static class Config {        @Bean        public CacheManager getCacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache(CACHE_NAME))); return cacheManager;        }        @Bean        public ICacheableService getMockedEntityService() { return new ICacheableService() {     private final Random random = new Random();     @Autowired     ApplicationContext context;     @Override     @Cacheable(value = CACHE_NAME, key = "#root.methodName.concat('_').concat(#p0)")     public String methodA(int length) {         methodInvocations.incrementAndGet();         System.out.println("Invoking methodA");         char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();         StringBuilder sb = new StringBuilder();         for (int i = 0; i < length; i++) {  sb.append(chars[random.nextInt(chars.length)]);         }         String result = sb.toString();         System.out.println("Returning " + result + " for length: " + length);         return result;     }     @Override     @Cacheable(value = CACHE_NAME, key = "#root.methodName.concat('_').concat(#p0)")     public String methodB(String name) {         methodInvocations.incrementAndGet();         System.out.println("Invoking methodB");         ICacheableService cache = context.getBean(ICacheableService.class);         String rand = cache.methodA(name.length());         String result = name + "_" + rand;         System.out.println("Returning " + result + " for name: " + name);         return result;     } };        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470078.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号