我正在使用JCache API在应用程序中配置缓存,该应用程序在Ehcache 3中使用弹簧缓存。
cacheManager.createCache("users", new MutableConfiguration<String, User>()
.setStoreByValue(false)
.setManagementEnabled(true)
.setStatisticsEnabled(true)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.TEN_MINUTES)));
Run Code Online (Sandbox Code Playgroud)
如何将缓存大小限制为50个条目?通过Ehcache XML配置很容易做到这一点,但是如果有一种使用JCache config API进行控制的方法,我宁愿使用它。
我是 Java 缓存的新手,我试图了解按值存储与按引用存储之间的区别。
我在下面引用了 java.cache 文档中的段落“复制存储在缓存中的条目以及从缓存返回时再次复制条目的目的是允许应用程序继续改变键和值的状态而不会导致侧面 -对缓存所持有的条目的影响。”
上面提到的“副作用”是什么?而在实践中我们又该如何选择存储方式呢?
下面的类中有一个方法:
@Override
@Transactional
@Cacheable(value = "products", key = "#id")
public Product getProduct(long id) throws ApplicationException {
Product product = null;
try {
ProductEntity productEntity = productDAO.getProduct(id);
product = productTransformer.toProduct(productEntity);
} catch (SystemException ex) {
throw new ApplicationException(ex.getCode(), ex.getMessage(), "Problem in DataLayer", "Data Layer Error",
new Object[] { ex });
}
return product;
}
Run Code Online (Sandbox Code Playgroud)
该应用程序运行良好。但我希望在数据放入缓存时有一个缓存命中日志。我想通过 log4j.properties 记录它。
如何配置 application.properties 以便可以记录它?
我尝试ehcache3在Spring 4.3的项目上使用。我配置了缓存管理器:
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
<property name="cacheManager">
<bean class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
<property name="cacheManagerUri" value="classpath:ehcache.xml"/>
</bean>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
和ehcache.xml:
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd" >
<service>
<jsr107:defaults enable-statistics="true" enable-management="true"/>
</service>
<cache alias="customerSettings">
<key-type>java.lang.Long</key-type>
<expiry>
<none/>
</expiry>
<resources>
<heap>500</heap>
</resources>
</cache>
</config>
Run Code Online (Sandbox Code Playgroud)
但是当我部署项目时,我有一个例外:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cache [customerSettings] specifies key/value types. Use getCache(String, Class, …Run Code Online (Sandbox Code Playgroud) 我想在应用程序重新启动后使缓存可用,并在配置中添加了以下行:
<disk unit="MB">100</disk>
Run Code Online (Sandbox Code Playgroud)
之后,当我启动应用程序时,我有以下堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [my/pack/EhcacheConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cacheManager' threw exception; nested exception is org.ehcache.StateTransitionException: Cache 'pow_cache' creation in EhcacheManager failed.
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at …Run Code Online (Sandbox Code Playgroud) 请不要将此问题标记为重复.我读过以前的问题,但我仍然无法理解.
我目前正在使用Java设计一个使用MongoDB进行持久化的项目.但是由于它的一些性能问题,我被要求使用Memcached.但我无法弄清楚Memcached如何帮助我做到这一点.
在浏览时,由于Memcache和Memcached等缓存服务,我更加困惑.有人可以解释一下这些有何不同,为什么当问到Memcached时,PHP会在一些问题中找到答案.
我请求所有人清楚地回答并通过一个例子告诉我如何将Memcached用于我的项目.什么是Memcache,Memcached,Jcache和SpyMemcached?
如果可能,请提供链接以完成某处的Memcached示例.
我正在尝试设置一个简单的示例应用程序来测试 hazelcast 的 jcache 实现。示例代码:
CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager();
Cache<String, String> m = cacheManager.getCache("test");
m.put("key1", "value1");
Run Code Online (Sandbox Code Playgroud)
在我的 pom.xml 文件中定义了 jcache 和 hazelcast
<dependencies>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0-PFD</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.6.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我收到此错误。无法弄清楚我应该在哪里/如何定义这个 ComepleteConfiguration 类。
Exception in thread "main" java.lang.NoClassDefFoundError: javax/cache/configuration/CompleteConfiguration
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at …Run Code Online (Sandbox Code Playgroud) 我已经检查了Jcache规范,我的理解是它在加载和从缓存中删除对象方面提供了更大的灵活性,但你必须编写自己的数据访问.
Hibernate L2缓存非常适合缓存来自RDBMS的数据,但在填充和驱逐方面提供的灵活性有限.
可以将两种功能中的最佳功能一起使用,以避免在Jcache实现中实现数据访问,例如可以满足两种规范的Ignite.
更精确的是,Ignite创建的单个缓存对象可以同时被视为Hibernate L2缓存和Jcache.
我想使用Jcache注释,并在需要时使用hibernate进行数据库操作
我正在编写我的hazelcast原型代码.运行阅读器时遇到以下错误.不确定我错过了什么.
SEVERE: [host1]:5701 [dev] [3.7.3] Service with name'hz:impl:cacheService' not found!
com.hazelcast.core.HazelcastException: Service with name 'hz:impl:cacheService' not found!
at com.hazelcast.spi.impl.NodeEngineImpl.getService(NodeEngineImpl.java:350)
at com.hazelcast.spi.Operation.getService(Operation.java:239)
at com.hazelcast.cache.impl.operation.PostJoinCacheOperation.run(PostJoinCacheOperation.java:44)
at com.hazelcast.internal.cluster.impl.operations.PostJoinOperation.run(PostJoinOperation.java:93)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:181)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl.run(OperationExecutorImpl.java:375)
at com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl.run(OperationServiceImpl.java:267)
at com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl.runOperationOnCallingThread(OperationServiceImpl.java:262)
at com.hazelcast.internal.cluster.impl.operations.FinalizeJoinOperation.runPostJoinOp(FinalizeJoinOperation.java:139)
at com.hazelcast.internal.cluster.impl.operations.FinalizeJoinOperation.run(FinalizeJoinOperation.java:104)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:181)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:396)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:117)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.run(OperationThread.java:102)
Caused by: com.hazelcast.spi.exception.ServiceNotFoundException: Service with name 'hz:impl:cacheService' not found!
... 14 more
Run Code Online (Sandbox Code Playgroud)
这是我的代码
public class Reader {
public static void main(String[] args) throws InterruptedException {
Config config = new Config();
config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(“host1”).setEnabled(true);
config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(“host2”).setEnabled(true);
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
HazelcastInstance hz …Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用 Hazelcast(JCache 标准)进行缓存的分布式应用程序。
我有一个用例,我应该对集群中的特定键加锁,以防止更新期间调用。
我知道 EhCache 有一个非常相似的东西,它叫做 acquireReadLockOnKey(Object key)。
如何使用 JCache 和/或 Hazelcast 实现这种锁定?
可以使用键来读取缓存.像这样 :
Collection<Integer> userIds = context.getUserDao().allUserIds();
for (Integer userId : userIds) {
User user = cache.getUserCache().get(userId);
System.out.println(user.toString());
}
Run Code Online (Sandbox Code Playgroud)
使用后者,它会将过期的加载到缓存中,然后显示它.
但要求是查看当前缓存中的所有内容.
jcache ×11
java ×6
caching ×5
hazelcast ×4
ehcache ×3
spring ×2
spring-cache ×2
hibernate ×1
ignite ×1
jsr107 ×1
memcached ×1
mongodb ×1
spring-boot ×1
spymemcached ×1