无法获得从Java引导使用memcached的好例子

我正在使用java启动我的发展。 现在我已经使用'EhCache'进行缓存,它直接从Java引导支持。 这是“进程中”缓存,即成为您的进程的一部分。 现在好了。 但是我的服务器将在不久的将来在多个节点上运行。 因此,想要切换到“Memcached”作为常见的缓存层。

花了很多时间后,我无法从java启动中使用Memcached。 我已经看到了“ Simple Spring Memcached ”,它接近我的要求。 但是仍然以Spring的方式给出了使用XML配置的例子。 Java引导尽可能不使用这种XML配置。 至少我不能将这个例子快速映射到java引导世界。

我想从Java引导中使用Memcahed(直接或通过缓存抽象层)。 如果有人指出我有一个相关的java启动例子,它会为我节省很多时间。

您可以在这里和这里找到一些使用Java配置而不是XML文件配置SSM的资料。 基本上你必须把所有bean的定义从XML移动到Java。

我已经接受@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 implements Serializable”。

你也可以检查Memcached Spring Boot库。 它使用Spring Cache抽象的 Memcached实现。

换句话说,您使用与其他Spring Cache实现一样的配置和相同的注释。 您可以在这里查看图书馆的使用情况。

在Kotlin和Java中也有示例项目。