【问题标题】:Spring Caching not working with MemcachedSpring 缓存不适用于 Memcached
【发布时间】:2017-03-29 19:54:29
【问题描述】:

我正在尝试让我的 Web 应用程序使用 REST API 功能运行并使用 Memcached 进行缓存。

在我部署应用程序时,Spring 不会引发错误。但是缓存不起作用。我使用了 spymemcached Java 客户端和 Spring 缓存。 有人可以告诉我哪里出错了吗? 这是我的根上下文的 sn-p

    <cache:annotation-driven />
<bean class="net.spy.memcached.MemcachedClient">
    <constructor-arg ref="serverList" type="java.util.List" />
</bean>
<context:component-scan base-package="com.restdemo.cache" />

<util:list id="serverList" value-type="java.net.InetSocketAddress">
    <bean class="java.net.InetSocketAddress">
        <constructor-arg value="127.0.0.1" type="java.lang.String" />
        <constructor-arg value="11211" type="int" />
    </bean>
</util:list>

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <bean name="coursesCache" class="com.restdemo.cache.Memcache">
            </bean>
        </set>
    </property>
</bean>

这是我实现的 Cache 接口

public class Memcache implements Cache {

@Autowired
MemcachedClient cache;

private final String name = "";

public void clear() {
    cache.flush();

}

public void evict(Object key) {
    this.cache.delete(key.toString());

}

public ValueWrapper get(Object key) {
    Object value = null;
    try {
        value = cache.get(key.toString());
    } catch (final Exception e) {
        e.printStackTrace();
    }
    if (value == null) {
        return null;
    }
    return new SimpleValueWrapper(value);
}

public <T> T get(Object arg0, Class<T> arg1) {
    // TODO Auto-generated method stub
    return null;
}

public <T> T get(Object arg0, Callable<T> arg1) {
    // TODO Auto-generated method stub
    return null;
}

public String getName() {
    return name;
}

public Object getNativeCache() {
    return cache;
}

public void put(Object key, Object value) {
    cache.set(key.toString(), 7 * 24 * 3600, value);

}

public ValueWrapper putIfAbsent(Object arg0, Object arg1) {
    // TODO Auto-generated method stub
    return null;
}
}

我在我的 RestController 中使用了 Cacheable 注解如下-

@GetMapping("/courses")
@Cacheable("coursesCache")
public List<Course> getCourses() {
    return courseDAO.getAllCourses();
}

我确信 Memcached 客户端创建得很好。否则,我在创建根上下文 bean 时遇到错误。但是缓存不起作用。任何帮助将不胜感激。

【问题讨论】:

    标签: spring caching memcached spring-cache spymemcached


    【解决方案1】:

    已经有将 memcached(spymemcached 或 xmemcached)与 Spring Cache 抽象相集成的可用解决方案:Simple Spring Memcached。考虑使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2013-06-23
      • 2014-07-10
      • 2019-05-02
      相关资源
      最近更新 更多