java 如何使用 Memcached 配置 Spring Boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44507525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:13:10  来源:igfitidea点击:

How to Configure Spring Boot with Memcached

javacachingspring-bootmemcached

提问by Rahul

I am new to Memcached. I need to configure my spring boot application with Memcached.

我是 Memcached 的新手。我需要使用 Memcached 配置我的 Spring Boot 应用程序。

I researched a lot on the topic but I could not find a documentation for the same. By default Spring boot uses Concurrent HashMap for caching but how do I configure Memcached.

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

I got this GitHub URL but I am not sure if this is the correct way and if so how do I use the same.

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

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

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

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

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

Update

更新

I have used this in my project now https://github.com/bmatthews68/memcached-spring-boot-starter.

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

Like this

像这样

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

but when i do a telnet of get defaultCache i get nothing Please help

但是当我执行获取 defaultCache 的 telnet 时,我什么也没得到请帮忙

采纳答案by Rahul Singh

Add this to your Gradle Dependencies

将此添加到您的 Gradle 依赖项

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

On top of your main Spring boot application where you @SpringBootApplicationthis annotation put this

在您的主要 Spring 启动应用程序之上,您@SpringBootApplication将此注释放在

@EnableMemcached

Then in your Component use the following

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

@Autowired
private MemcachedClient memcachedClient;

memcachedClient.get("...")

回答by Igor

I'm one of the authors of the https://github.com/sixhours-team/memcached-spring-boot. The library will auto-configure Memcached within a Spring Boot application. You can enable it just as you would with Spring Cache i.e. it is sufficient to add the @EnableCachingannotation in your configuration class e.g.

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

@Configuration
@EnableCaching
public class CacheConfiguration {
}

The configuration in the application.ymlcan be as simple as:

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

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

At the moment, the library has not been released yet (the first release should be in about a week). You can find more info hereor check the demo Spring Boot app here.

目前,该库尚未发布(第一个版本应该在一周左右)。你可以找到更多的信息在这里或检查演示春天启动的应用程序在这里

One more thing, in order to support cache eviction, the library is prefixed with memcached:spring-boot:defaultCache:[radnom_number], so in your case the key would be something like e.g.

还有一件事,为了支持缓存逐出,库的前缀是memcached:spring-boot:defaultCache:[radnom_number],所以在你的情况下,关键是像

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

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

where 283is the random number assigned to the cache key (needed for the proper cache eviction).

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

回答by zeagord

The first GitHub project you have shown is a good solution. It is also a fork a spymemcached which is one of the prominent client libraries of Memcached.

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

Please refer the below official documentation. http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_caching

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

You can also check the below one and traverse to Getting Started page.

您还可以查看下面的一个并跳转到入门页面。

https://github.com/killme2008/xmemcached

https://github.com/killme2008/xmemcached