使用 memcached 的 Spring 缓存 [英] Spring cache using memcached

查看:63
本文介绍了使用 memcached 的 Spring 缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 https://github.com/AKQABER/simple-spring-memcached 将 memcached 作为缓存实现与 spring 集成.

I'm using https://github.com/AKQABER/simple-spring-memcached to have memcached integrated as a cache implementation with spring.

但是我没有成功:P

我使用的是 spring 4 和 spring-data-jpa

I'm using spring 4 and spring-data-jpa

public interface FooRepository extends JpaRepository<Foo, Long> {

@Cacheable(value = "defaultCache", key = "lala")
    Foo findByNameAndDescription(String name, String description);
} 

为了测试目的,我对密钥进行了硬编码.没什么特别的,使用名为defaultCache"的缓存.memcached的配置是:

I hardcoded the key for test purposes. Nothing special, use a cache called "defaultCache". The configuration of memcached is:

@Configuration
@EnableAspectJAutoProxy
public class CacheConfiguration
{

@Bean
public CacheManager cacheManager()
{
    MemcacheClientFactoryImpl cacheClientFactory = new MemcacheClientFactoryImpl();
    AddressProvider addressProvider = new DefaultAddressProvider("127.0.0.1:11211");
    com.google.code.ssm.providers.CacheConfiguration cacheConfiguration = new com.google.code.ssm.providers.CacheConfiguration();
    cacheConfiguration.setConsistentHashing(true); //TODO check this

    CacheFactory cacheFactory = new CacheFactory();
    cacheFactory.setCacheName           ( "defaultCache"        );
    cacheFactory.setCacheClientFactory  ( cacheClientFactory    );
    cacheFactory.setAddressProvider     ( addressProvider       );
    cacheFactory.setConfiguration       ( cacheConfiguration    );

    Cache object = null;
    try {
        object = cacheFactory.getObject();
    } catch (Exception e) {
        e.printStackTrace();
    }

    SSMCache ssmCache = new SSMCache(object, 10000, true); //TODO be very carefully here, third param allow remove all entries!!

    ArrayList<SSMCache> ssmCaches = new ArrayList<SSMCache>();
    ssmCaches.add(0, ssmCache);

    SSMCacheManager ssmCacheManager = new SSMCacheManager();
    ssmCacheManager.setCaches(ssmCaches);


    return ssmCacheManager;
}
}

我可以使用一个简单的主类和代码来测试它:

And I can test it using a simple main class with code:

CacheManager cacheManager = context.getBean(CacheManager.class);
    FooRepository repository = context.getBean(FooRepository.class);

    Cache defaultCache = cacheManager.getCache("defaultCache");
    defaultCache.clear();
    Foo byNameAndDescription = repository.findByNameAndDescription(DEFAULT_NAME,    DEFAULT_DESCRIPTION);
    Object obj = defaultCache.get("lala");

并且 obj 始终为空.但是,如果我直接使用 defaultCache 对象,我可以毫无问题地将项目放入/从缓存中获取.有什么想法吗?

And obj is always null. However if I use defaultCache object directly I can put and get items to/from cache without problems. Any idea?

推荐答案

您是否在您的应用程序中启用了 Spring Cache?如果没有,则在配置 bean 上使用 @EnableCaching.

Have you enabled Spring Cache in you application? If not then use @EnableCaching on the configuration bean.

如果您使用 org.springframework.cache.support.SimpleCacheManager 而不是 SSMCacheManager 来检查缓存是否工作无济于事.如果没有,则说明 Spring Cache 配置仍然存在问题.

If it doesn't help check if the caching work if you use org.springframework.cache.support.SimpleCacheManager instead of SSMCacheManager. If not then there is still a problem with Spring Cache configuration.

这篇关于使用 memcached 的 Spring 缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆