如何配置 Spring 使用 Memcached 启动

How to Configure Spring Boot with Memcached

我是 Memcached 新手。我需要使用 Memcached 配置我的 spring 引导应用程序。

我对这个主题进行了很多研究,但找不到相同的文档。 默认情况下 Spring 引导使用 Concurrent HashMap 进行缓存,但我该如何配置 Memcached。

我得到了这个 GitHub URL 但我不确定这是否是正确的方法,如果是这样我该如何使用它。

https://github.com/sixhours-team/memcached-spring-boot

https://www.javacodegeeks.com/2013/06/simple-spring-memcached-spring-caching-abstraction-and-memcached.html

更新

我现在已经在我的项目中使用了这个https://github.com/bmatthews68/memcached-spring-boot-starter

像这样

@Override @Cacheable(value = "defaultCache")
    public String customMethof() throws InterruptedException {
        return "Testing";
    }

但是当我使用 telnet 获取 defaultCache 时,我什么也没得到 请帮助

您展示的第一个 GitHub 项目是一个很好的解决方案。它也是 spymemcached 的一个分支,它是 Memcached 的著名客户端库之一。

请参考以下官方文档。 http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_caching

您也可以查看下面的内容并转到入门页面。

https://github.com/killme2008/xmemcached

将此添加到您的 Gradle 依赖项

compile group: 'net.spy', name: 'spymemcached', version: '2.12.3'
compile('com.btmatthews.springboot:memcached-spring-boot-starter:1.0.0')

在您的主 Spring 引导应用程序之上,您 @SpringBootApplication此注释将此

@EnableMemcached

然后在您的组件中使用以下内容

@Autowired
private MemcachedClient memcachedClient;

memcachedClient.get("...")

我是 https://github.com/sixhours-team/memcached-spring-boot 的作者之一。 该库将在 Spring 启动应用程序中自动配置 Memcached。您可以像使用 Spring 缓存一样启用它,即在您的配置 class 中添加 @EnableCaching 注释就足够了,例如

@Configuration
@EnableCaching
public class CacheConfiguration {
}

application.yml中的配置可以很简单:

memcached.cache:
   servers: example1.com:11211
   mode: static
   expiration: 86400

目前库还没有发布(第一次发布应该在一周左右)。您可以找到更多信息 here or check the demo Spring Boot app here.

还有一件事,为了支持缓存 逐出 ,该库以 memcached:spring-boot:defaultCache:[radnom_number] 为前缀,因此在您的情况下,密钥将类似于例如

memcached:spring-boot:books:defaultCache:283:SimpleKey[]

其中 283 是分配给缓存键的随机数(正确缓存逐出所需)。