- 浏览: 468346 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (146)
- Maven (3)
- Quartz (10)
- Hessian (3)
- JDK (42)
- eclipse (4)
- 设计思想 (6)
- XML (8)
- JavaMail (1)
- Spring (11)
- mina (1)
- HsqlDb (1)
- Cache (2)
- Tool (6)
- 心情 (5)
- JQuery (0)
- Hadoop (5)
- Hbase (3)
- 自动构建 (7)
- JNDI (0)
- 代码赏析 (5)
- Oracle (1)
- Excel (4)
- Effective Java (5)
- JAXB (4)
- fdafasdf (1)
- ccc (0)
- web (3)
- concurrent (1)
- CVS (1)
- eclipse plugin (2)
- Apache (10)
最新评论
-
chxiaowu:
nice!
Quartz实现固定执行次数 -
zxjlwt:
学习了。http://surenpi.com
自定义ClassLoader -
kadlly:
public static final Logger log ...
Hessian 权限认证 -
spring_springmvc:
java程序语言学习教程 地址http://www.zuida ...
Java-Final -
liushuiwuyan:
[img][/img]
设计模式-单例
Application Cache is used very wide.
we need to cache user/business information in application, cause of it is used often, so don't need to clear cache.
sure, we can control of it, but if we cache so many messages, we will be lose control. every business want to cache something to improve it's performance, so what's the solution?
we can use soft reference, it will be GC before out of memory, and it used cache in so many cache framework.
we need to cache user/business information in application, cause of it is used often, so don't need to clear cache.
sure, we can control of it, but if we cache so many messages, we will be lose control. every business want to cache something to improve it's performance, so what's the solution?
we can use soft reference, it will be GC before out of memory, and it used cache in so many cache framework.
package com.statestreet.tlp.cache; import java.lang.ref.SoftReference; import java.util.Collections; import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * Common cache use scenarios include an application cache, a second level (L2)[EHCache] cache and a hybrid cache. * * * Global Cache.[Application Cache] * * Strong ==> Soft ==> Weak ==> Phantom * * SoftReference: Soft references are most often used to implement memory-sensitive caches * Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand. * * WeakHashMap: the key is not usually use will be delete, there map is not synchronized {@link Collections#synchronizedMap Collections.synchronizedMap}. * case of back thread delete object don't use, so it is not used abroad * PhantomReference: garbage collector will be delete object, phantom reference always returns <code>null</code>. * almost we can't see this example. * * * @author e557400 * */ public class GlobalCache { protected final Log log = LogFactory.getLog(GlobalCache.class); private static final GlobalCache gc = new GlobalCache(); /** * Application Cache don't clear, until we call. */ private ConcurrentHashMap<CacheKey, SoftReference<Object>> cache = new ConcurrentHashMap<CacheKey, SoftReference<Object>>(); private ConcurrentHashMap<CacheKey, Integer> cachehitCount = new ConcurrentHashMap<CacheKey, Integer>(); private GlobalCache(){ } public void clear(){ cache.clear(); cachehitCount.clear(); } public static GlobalCache getInstance(){ return gc; } /** * Thread-safe cache put. * * cache get method is not thread-safe, invalidate by other thread. * but map.putIfAbsent() which is atomic and therefore thread-safe. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, * or <tt>null</tt> if there was no mapping for the key */ public Object put(CacheKey key, Object value) { if(value == null){ throw new IllegalArgumentException("put GlobalCache value can't be null"); } Object ret = get(key); if (value != ret) { if(log.isDebugEnabled()){ if(ret == null){ log.debug("put new Cache( key["+key+"], value["+value+"] )"); }else{ log.error("attempt to override Cache( key["+key+"], old value["+ret+"] to value["+value+"] ), but will be failed."); } } ret = cache.putIfAbsent(key, new SoftReference<Object>(value));//if already associated with special key, no override. if (ret == null) { ret = value; } } return ret; } /** * ConcurrentHashMap call get method will be get segent lock * * @param key * @return */ public Object get(CacheKey key){ Object value = null; SoftReference<Object> valueref = cache.get(key); if(valueref != null){ value = valueref.get(); if(value == null){ // If the value has been garbage collected, remove the entry from the HashMap. cache.remove(key); } } // log cache and monitor. if(log.isDebugEnabled()){ Integer count = cachehitCount.get(key); if(count == null){ count = 1; } if(valueref != null){ log.debug("cache hit key["+key+"], count["+count+"]"); count ++; cachehitCount.put(key, count); } } return value; } /** * wrap * @param key * @param value * @return */ public Object put(String key, Object value){ return put(new CacheKey(key),value); } /** * value * @param value * @return */ public Object get(String key){ return get(new CacheKey(key)); } }
发表评论
-
Java 字符串分词
2015-01-02 14:43 1749在Java的世界里有个类型 ... -
jdk 1.6 新特性,集成Groovy, 性能很差
2014-04-02 14:27 1276性能都是相对的,如果调用量不是很大的话,可以忽略,毕竟使用为主 ... -
Fake Code easy implements
2014-04-01 15:41 1026package org.miniframe.modules ... -
JDK regex 用法及用途
2014-03-31 15:48 1214查找 Boolean flag = pattern.mat ... -
生产者消费者(四)
2014-03-04 12:32 1147需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
生产者消费者(三)
2014-03-04 10:59 959需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
生产者消费者(二)
2014-03-03 15:40 694需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
生产者消费者模式(一)
2014-02-28 14:30 1029需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓 ... -
查看Class文件使用的JDK版本
2013-10-30 14:17 1115由于JDK一般是向下兼容的,所以有时候本地的JDK版本比类库的 ... -
Java源代码转码
2012-12-20 17:22 1323现在中国的项目很多,编码无非是UTF-8,GBK,GB2312 ... -
Tomcat集成OSGI,并通过JNDI开放Web调用
2012-12-03 11:22 3134Tomcat集成OSGi,首先要选择OSGI服务器,我这里采用 ... -
JDK的Logging
2012-11-07 15:49 1683jdk自带有一个log日志,对于一般的使用,仅够了. 代码如下 ... -
java.util.*
2012-11-06 14:23 1377java.util 工具包,灰常的有用,有机会一定要研读源码。 ... -
java.util.concurrent.*
2012-11-02 10:38 17761. java.util.concurrent.ArrayBl ... -
java.util.rt.*
2012-10-31 13:51 11131. java.util.HashMap 散列表,主要是以离散 ... -
巧秒设计方法,不返回null
2016-09-27 19:32 722/** * {@inheritDoc} * ... -
java doc 代码文档
2012-07-13 13:58 1330对于代码规范不解释了,网上很多。 在编写代码的时候,有一点灰 ... -
接口与抽象类
2012-07-11 16:53 11241. 接口设计必谨慎,除非业务变更,否则打死不能动接口。[不变 ... -
JVM优化机制好诡异
2012-04-20 08:43 1466long i[] = new long[1000000]; ... -
JVM优化机制好诡异
2016-09-27 19:32 561long i[] = new long[100000 ...
相关推荐
3. **Java Application Cache**:Oracle 9i提供了一个Java应用程序缓存,可以在数据库级别存储和共享数据,提高了应用程序的性能。 4. **JDBC(Java Database Connectivity)**:Oracle 9i优化了JDBC驱动,提供了...
配置完成后,你需要在SpringBoot的配置文件(application.yml或application.properties)中设置JetCache的相关参数。例如,如果你想使用Redis作为缓存后端,可以这样配置: ```yaml jetcache: local: limit: ...
5. **CacheApplication.java**:这是Spring Boot应用的主类,通常包含`@SpringBootApplication`注解,启动整个应用。可能还包含了一些初始化逻辑,如配置缓存或者启动时的其他自定义操作。 6. **缓存策略**:在Java...
http://localhost/applicationcache/zepto.js NETWORK: 4.jpg FALLBACK: *.html /offline.html 2.jpg /3.jpg ``` 上述`manifest`文件分为三部分: 1. `CACHE`:列出需要缓存的文件。 2. `NETWORK`:列出需要在...
### 更简单的Java缓存框架 jscache #### 知识点概述 jscache是一个轻量级、易用的Java缓存框架,它基于面向切面编程(AOP)原理实现,支持灵活配置缓存策略,并提供了多种缓存实现方式。通过使用jscache,开发者...
然后,在application.properties文件中配置JetCache的相关参数: ```properties jetcache.remote.default.valueDecoder = java jetcache.remote.default.keyConvertor = fastjson jetcache.areaInCacheName = false...
配置Redis连接并启用J2Cache的Redis支持,我们可以在`application.properties`中添加以下内容: ``` spring.redis.host=localhost spring.redis.port=6379 j2cache.cacheManager.type=redis ``` 在使用J2Cache的...
2. 配置Redis:在`application.properties`或`application.yml`中配置Redis连接信息,包括地址、端口、密码等。 ```properties spring.redis.host=localhost spring.redis.port=6379 ``` 3. 创建RedisCacheManager...
add_header Cache-Control no-cache; types { application/vnd.apple.mpegurl m3u8; video/mp4 ts; } default_type application/vnd.apple.mpegurl; } } } ``` HTML5的`<video>`标签支持HLS流播放,只需...
**Redis整合SpringCache实例** 在现代的Web应用中,数据缓存是提高系统性能的关键技术之一。本示例主要探讨如何将开源的内存数据结构存储系统Redis与Spring Cache框架结合,实现高效的分布式缓存解决方案。Redis以...
综上所述,这个“Java Spring Boot application for currency conversion”项目涵盖了Spring MVC框架的使用、单元测试的实践以及测试结果的可视化,体现了良好的软件工程原则和最佳实践。无论是对初学者还是经验丰富...
这包括选择合适的缓存提供商,比如EhCache,然后在Hibernate的配置文件(hibernate.cfg.xml或application.properties)中启用二级缓存并指定对应的缓存策略。例如: ```xml <property name="hibernate.cache.use_...
7. **事件监听**:Spring Cache还支持缓存事件监听,通过实现`ApplicationListener<CacheEvictedEvent>`等接口,可以在缓存操作后执行相应的逻辑。 8. **异常处理**:当方法抛出异常时,Spring Cache可以配置是否将...
首先,J2Cache是一款轻量级的Java缓存框架,它支持多级缓存策略,可以方便地与其他缓存实现如Ehcache和Redis结合使用。Ehcache作为一级缓存,主要用于本地快速访问,而Redis作为二级缓存,用于跨服务器的数据共享和...
标题中的“websphere缓存java调用以及jar包”指的是在IBM WebSphere Application Server (WAS) 中使用Java编程方式来管理和操作缓存系统。WebSphere应用服务器提供了一种高效的方式来存储和检索频繁访问的数据,以...
接下来,配置文件`application.yml`中,我们可以使用Redis的默认配置,如果需要自定义配置,例如连接池、密码等,可以按需添加。 在应用中启用Spring Cache,我们需要创建一个配置类,该类使用`@EnableCaching`注解...
Monitor your applicationsApplication optimization: memory management and server configurationScale up: threading and implicationsBe lazy, cache your dataBe fault tolerantLoggers and performances: a ...
mmp-application- cache-包含Java类和资源,这些类和资源提供基于Hazelcast的分布式内存中缓存功能。 mmp-application-kafka-包含支持使用Apache Kafka开发应用程序的Java类和资源。 mmp-application-messaging-...