首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WildFly 11/Java 7/JSR 107:缓存-缓存信息并使其自动过期的最佳方法是什么?

WildFly 11/Java 7/JSR 107:缓存-缓存信息并使其自动过期的最佳方法是什么?
EN

Stack Overflow用户
提问于 2017-10-14 22:47:24
回答 2查看 1.5K关注 0票数 2

在Java /WebApp容器中存储数据片段并让其自动过期的推荐方法是什么?我可以使用会话持久性机制,但是我的http会话通常比我希望保留这些信息片段的时间长得多。

Java 7或CDI提供了什么吗?JCache规范JSR 107的一些初步变体?还有其他好办法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-15 19:12:17

我不确定这是“最好”的方式,但我一直在使用谷歌番石榴缓存在野生苍蝇(8到10,但希望仍然适用)。对我来说,我缓存Oauth令牌是因为一个非常慢的auth服务器。我的代码看起来像是:

代码语言:javascript
复制
private static LoadingCache<String, MyPrincipal> tokenCacheMap;

@PostConstruct
private void postConstruct() {
    tokenCacheMap = CacheBuilder.newBuilder()
            .expireAfterAccess(15, TimeUnit.MINUTES)
            .build(
                    new CacheLoader<String, MyUserPrincipal>() {
                        @Override
                        public MyUserPrincipal load(String token) {
                            MyUserPrincipal myUserPrincipal = getUserFromToken(token);

                            if( myUserPrincipal != null ) {
                                myUserPrincipal.setToken(token);
                                return myUserPrincipal;
                            }

                            throw new SecurityException("token is not valid");
                        }
                    }
            );
}

//
// later in the code...
//
MyUserPrincipal myUserPrincipal = tokenCacheMap.get(token);

基本上,它所做的是设置一个缓存,令牌在其中驻留15分钟。如果需要,将调用load()方法,在本例中,获取一个auth令牌和用户。缓存是在需要时懒洋洋地填充的--第一个调用将有获取令牌的开销,但之后将全部存储在内存中。

还有其他选项,例如,根据缓存中的项数来删除旧信息。文档很好,应该能让你走。

缺点是这不是一个JEE标准,但它在过去对我起过作用。

票数 3
EN

Stack Overflow用户

发布于 2018-01-20 09:37:16

如果您想使用infinispan-jcache API,可以使用JCache。英菲尼西是一个可扩展的、高度可用的密钥/值数据存储包括在WildFly中

使用它,将infinispan-jcache添加到pom.xml

代码语言:javascript
复制
<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-jcache</artifactId>
    <version>...</version> <!-- e.g. 9.1.4.Final -->
</dependency>

并访问缓存如下所示

代码语言:javascript
复制
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.CompleteConfiguration;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.spi.CachingProvider;
import java.util.concurrent.TimeUnit;

...

// Construct a simple local cache manager with default configuration
// and default expiry time of 5 minutes.
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
CompleteConfiguration<String, String> configuration = new 
MutableConfiguration<String, String>()
            .setTypes(String.class, String.class)
            .setExpiryPolicyFactory(factoryOf(new CreatedExpiryPolicy(
                    new Duration(TimeUnit.MINUTES, 5))));

// Create a cache using the supplied configuration.
Cache<String, String> cache = cacheManager.createCache("myCache", configuration);

// Store a value, the entry will expire in 2 seconds.
cache.put("key", "value", 2, TimeUnit.SECONDS);
// Retrieve the value and print it out.
System.out.printf("key = %s\n", cache.get("key"));

// Stop the cache manager and release all resources,
// use try-with-resources in real code.
cacheManager.close();

请注意,Infinispan有伟大的博士

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46750063

复制
相关文章

相似问题

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