1

我正在使用java boot进行开发。现在我使用“EhCache”进行缓存,它直接从 Java 引导中得到支持。这是“进程内”缓存,即成为您进程的一部分。现在没关系。但我的服务器将在不久的将来在多个节点上运行。因此希望切换到“Memcached”作为通用缓存层。

在花费大量时间之后,我无法从 java boot 中获得使用 Memcached 的良好示例。我看过' Simple Spring Memcached ',它接近我的要求。但它仍然给出了以 Spring 方式使用 XML 配置的示例。Java boot 尽量不使用这样的 XML 配置。至少我无法将示例快速映射到 java boot world。

我想从 java boot 中使用 Memcahed(直接或通过缓存抽象层)。如果有人向我指出相关的 java 引导示例,它将为我节省大量时间。

4

3 回答 3

2

您还可以查看Memcached Spring Boot库。它使用Memcached实现Spring Cache Abstraction

换句话说,您使用与任何其他 Spring Cache 实现相同的配置和相同的注释。您可以在此处查看该库的使用情况。

KotlinJava中也有示例项目。

于 2017-09-19T10:56:04.440 回答
1

我已经接受了@ragnor 给出的答案。但我认为我应该在这里发布一个对我有用的完整示例。

  1. 通过添加 @EnableCaching 确保您的应用程序已启用缓存
  2. POM.xml 应具有以下依赖项:
<dependency>
        <groupId>com.google.code.simple-spring-memcached</groupId>
        <artifactId>spring-cache</artifactId>
        <version>3.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.simple-spring-memcached</groupId>
        <artifactId>spymemcached-provider</artifactId>
        <version>3.6.1</version>
    </dependency>
  1. 添加一个配置文件来配置你的 memcached 缓存配置,比如 MySSMConfig.java
@Configuration
@EnableAspectJAutoProxy
@ImportResource("simplesm-context.xml") // This line may or may not be needed,
                                        // not sure
public class SSMConfig 
{
    private String _memcachedHost; //Machine where memcached is running
    private int _memcachedPort;    //Port on which memcached is running

     @Bean
     public CacheManager cacheManager() 
     {
         //Extended manager used as it will give custom-expiry value facility in future if needed
         ExtendedSSMCacheManager ssmCacheManager = new ExtendedSSMCacheManager();

         //We can create more than one cache, hence list
         List<SSMCache>cacheList = new ArrayList<SSMCache>();

         //First cache: Testcache
         SSMCache testCache = createNewCache(_memcachedHost, _memcachedPort, 
                                    "testcache", 5);

         //One more dummy cache
         SSMCache dummyCache = createNewCache(_memcachedHost,_memcachedPort, 
                    "dummycache", 300);

         cacheList.add(testCache);
         cacheList.add(dummyCache);

         //Adding cache list to cache manager
         ssmCacheManager.setCaches(cacheList);

         return ssmCacheManager;
     }


    //expiryTimeInSeconds: time(in seconds) after which a given element will expire
    //
    private SSMCache createNewCache(String memcachedServer, int port, 
                                String cacheName, int expiryTimeInSeconds)
    {
        //Basic client factory to be used. This is SpyMemcached for now.
        MemcacheClientFactoryImpl cacheClientFactory = new MemcacheClientFactoryImpl();

        //Memcached server address parameters
        //"127.0.0.1:11211"
        String serverAddressStr = memcachedServer + ":" + String.valueOf(port);
        AddressProvider addressProvider = new DefaultAddressProvider(serverAddressStr);

        //Basic configuration object
        CacheConfiguration cacheConfigToUse = getNewCacheConfiguration();

        //Create cache factory
        CacheFactory cacheFactory = new CacheFactory();
        cacheFactory.setCacheName(cacheName);
        cacheFactory.setCacheClientFactory(cacheClientFactory);
        cacheFactory.setAddressProvider(addressProvider);
        cacheFactory.setConfiguration(cacheConfigToUse);

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

        }

        //allow/disallow remove all entries from this cache!!
        boolean allowClearFlag = false;
        SSMCache ssmCache = new SSMCache(object, expiryTimeInSeconds, allowClearFlag); 

        return ssmCache;

    }

    private CacheConfiguration getNewCacheConfiguration() 
    {
        CacheConfiguration ssmCacheConfiguration = new CacheConfiguration();
        ssmCacheConfiguration.setConsistentHashing(true);
        //ssmCacheConfiguration.setUseBinaryProtocol(true);
        return ssmCacheConfiguration;
    }

}
  1. 好的,我们已经准备好使用我们配置的缓存了。
  2. 其他类中用于从缓存中读取和从缓存中删除的示例方法
@Cacheable(value="dummycache, key="#givenId.concat('-dmy')", unless="#result == null")
    public String getDummyDataFromMemCached(String givenId)
    {
        logger.warn("getDummyDataFromMemCached: Inside DUMMY method to actually get data");
        return "Sample-" + String.valueOf(givenId);
    }
    @CacheEvict(value="dummycache",key="#givenId.concat('-dmy')")
    public void removeDummyDataFromMemCached(String givenId)
    {
        //Do nothing
        return;
    }
  1. 请注意,我们为 kache-keys 添加了后缀。由于 Memcached 不支持缓存区域,因此“dummycache”和“testcache”最终不会在单个服务器上保持独立。(它们可能与其他一些缓存实现分开)。因此,为了避免冲突,我们将唯一的后缀附加到缓存键。

  2. 如果要缓存自己类的对象,请确保它们是可序列化的。只需将您的类定义更改为“XYZ 实现可序列化”。

于 2017-06-13T07:49:41.937 回答
0

您可以在此处此处找到一些如何使用 Java 配置而不是 XML 文件来配置 SSM 的材料。基本上,您必须将所有 bean 的定义从 XML 移动到 Java。

于 2017-06-08T04:58:35.487 回答