ehcache是​​否为所有客户端使用相同的缓存 [英] Does ehcache uses same cache for all clients

查看:86
本文介绍了ehcache是​​否为所有客户端使用相同的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ehcache缓存。对于两个不同的用户,我的Web服务会创建不同的缓存,其作用类似于会话。我可以创建与上下文概念相同的单个缓存吗?

I am trying to use ehcache cache. For two different user my webservice creates different cache which is acting like session. Can i create single cache which will work same as context concept

推荐答案

实例化EhCache CacheManager时(这是获取缓存的第一步)每个EhCache API都有几个选项:

When instantiating an EhCache CacheManager (which is the first step in getting a reference to a Cache object) you have several options per EhCache APIs:


  1. 传统对象构造函数:new CacheManager()。如API页面中所述,此方法不能用作单例。调用者必须维护对其的引用。

  2. 静态构造函数:CacheManager.create()。这种创建方法确实充当单例,这意味着您可以为每个请求或会话调用CacheManager.create(),并且它将始终返回相同的CacheManager。

由于您尚未提供有关实现的任何详细信息,因此我只能假设您(无论是否知道)正在创建一个新的CacheManager对象( http://ehcache.org/apidocs/net/sf/ehcache/CacheManager.html )或会话...这将解释您注意到的行为。

Since you haven't provided any detail on your implementation, I can only suppose that you are (knowingly or not) creating a new CacheManager object (http://ehcache.org/apidocs/net/sf/ehcache/CacheManager.html) per service request or session...and that would explain the behavior you're noticing.

如果您使用单例概念创建CacheManager(方法2),则确实可以创建单个CacheManager +获得对可以跨线程(请求,会话等)访问的Cache对象的单个引用

If you were using the singleton concept for creating the CacheManager (method 2), you would indeed create a single CacheManager + get a single reference to a Cache object that can be access accross threads (requests, sessions, etc ...)

这篇关于ehcache是​​否为所有客户端使用相同的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆