首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法获得从Java引导中使用memcached的良好示例

无法获得从Java引导中使用memcached的良好示例
EN

Stack Overflow用户
提问于 2017-06-07 12:55:02
回答 3查看 2.4K关注 0票数 1

我在我的开发中使用了java引导。目前,我已经使用'EhCache‘进行缓存,Java直接支持它。这是“进程中”缓存,即成为进程的一部分。暂时没问题。但在不久的将来,我的服务器将在多个节点上运行。因此,希望切换到'Memcached'作为公共缓存层。

在花费了大量时间之后,我无法获得从java引导中使用Memcached的良好示例。我看过“简单Spring Memcached”,它接近我的要求。但是,它还是给出了使用Spring方式使用XML配置的示例。Java引导尽可能不使用这种XML配置。至少我不能将这个示例快速映射到java引导世界。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-06-08 04:58:35

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

票数 0
EN

Stack Overflow用户

发布于 2017-09-19 10:56:04

您还可以检查Memcached Spring Boot库。它使用Memcached实现来实现Spring抽象。

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

KotlinJava中也有一些示例项目。

票数 2
EN

Stack Overflow用户

发布于 2017-06-13 07:49:41

我已经接受了@ragnor的回答。但我想我应该在这里发表一个对我有用的完整的例子。

  1. 通过添加@EnableCaching,确保为应用程序启用了缓存
  2. POM.xml应该具有以下依赖性:
代码语言:javascript
复制
<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
代码语言:javascript
复制
@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. 从缓存中读取和从缓存中删除的其他类中的示例方法
代码语言:javascript
复制
@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-键中添加了后缀。由于Memcached不支持缓存区域,因此"dummycache“和"testcache”最终不会在单个服务器上保持分离。(它们可能与其他缓存实现分离)。因此,为了避免冲突,我们将唯一的后缀添加到缓存键中。
  2. 如果要缓存自己类的对象,请确保它们是可序列化的。只需将类定义更改为“XYZ实现序列化”即可。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44413283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档