简体   繁体   English

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

[英]WildFly 11/Java EE 7/JSR 107: caching - what's the best way to cache information and let it expire automatically?

What's the recommended way to store pieces of data and let it expire automatically from within a Java EE/WebApp container?在 Java EE/WebApp 容器中存储数据片段并让它自动过期的推荐方法是什么? I could use the session persistence mechanism, but my http sessions are usually A LOT longer than I want those pieces of information to be retained.我可以使用会话持久性机制,但我的 http 会话通常比我希望保留这些信息的时间长很多。

Is there something provided by Java EE 7 or by CDI? Java EE 7 或 CDI 是否提供了一些东西? Some preliminary variant of the JCache spec JSR 107? JCache 规范 JSR 107 的一些初步变体? Any other good solution?还有什么好的解决办法吗?

I'm not sure it is the "best" way but I've been using the Google Guava cache in Wildfly (8 through 10 but hopefully still applicable).我不确定这是“最佳”方式,但我一直在 Wildfly 中使用Google Guava 缓存(从 8 到 10,但希望仍然适用)。 For me I'm caching Oauth tokens because of a very slow auth server.对我来说,由于身份验证服务器非常慢,我正在缓存 Oauth 令牌。 My code looks something like:我的代码看起来像:

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);

Basically what this does is set up a cache where the tokens live for 15 minutes.基本上,它的作用是设置一个缓存,令牌可以在其中保存 15 分钟。 If needed, the load() method is called to, in this case, get an auth token and user.如果需要,调用load()方法,在这种情况下,获取身份验证令牌和用户。 The cache is lazily filled as needed - the first call will have the overhead of getting the token but after that it's all in memory.缓存根据需要延迟填充 - 第一次调用将有获取令牌的开销,但之后它全部在内存中。

There are other options to, for example, evict old information based on the number of items in the cache.例如,还有其他选项可以根据缓存中的项目数量驱逐旧信息。 The documentation is very good and should get you going.文档非常好,应该可以帮助您。

The downside is that this is not a JEE standard but it has worked for me in the past.缺点是这不是 JEE 标准,但它在过去对我有用。

You can use infinispan-jcache if you want to use the JCache API.如果你想使用 JCache API,你可以使用infinispan-jcache Infinispan is a scalable, highly available key/value data store included in WildFly . InfinispanWildFly 中包含的可扩展、高度可用的键/值数据存储。

To use it , add infinispan-jcache to pom.xml : 要使用它,请将infinispan-jcache添加到pom.xml

<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-jcache</artifactId>
    <version>...</version> <!-- e.g. 9.1.4.Final -->
</dependency>

and access the cache as follows :并按如下方式访问缓存:

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();

Note that Infinispan has great docs .请注意,Infinispan 有很棒的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM