我正在使用 java boot 进行开发。现在我使用过EhCache'对于缓存,它直接受Java启动支持。这是"进程中"缓存,即成为您的过程的一部分。现在没关系。但我的服务器将在不久的将来在多个节点上运行。因此,想要切换到' Memcached' 作为常见的缓存层。
花了很多时间后,我无法从java启动中获得使用Memcached的好样本。我看过' Simple Spring Memcached'这接近我的要求。但它仍然以Spring方式使用XML配置给出了示例。 Java引导尽可能不使用此类XML配置。至少我无法将示例快速映射到java启动世界。
我想从java boot中使用Memcahed(直接或通过cache-abstraction-layer)。如果有人指出我相关的java启动示例,它将为我节省大量时间。
答案 0 :(得分:2)
您还可以查看Memcached Spring Boot库。它使用 Memcached 实现 Spring Cache Abstraction 。
换句话说,您使用与任何其他Spring Cache实现相同的配置和相同的注释。您可以查看here图书馆的使用情况。
答案 1 :(得分:1)
我已经接受了@ragnor给出的答案。但我想我应该在这里发布一个对我有用的完整例子。
<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>
@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;
}
}
@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;
}
请注意,我们在kache-keys中添加了后缀。由于Memcached不支持缓存区,因此“dummycache”和“testcache”最终不会在单个服务器上保持独立。 (它们可能与其他一些缓存实现保持分离)。因此,为了避免冲突,我们在缓存键中添加了唯一的后缀。
如果要缓存自己类的对象,请确保它们是可序列化的。只需将您的班级定义更改为“XYZ implements Serializable”。
答案 2 :(得分:0)