OSCache是一个工业级的J2EE缓存实现。
它可以缓存 :
1 java对象,
2 页面
3 http请求
4 二进制内容:例如PDF文件
等
正文开始 。。。
为了减少与数据库通信来提高应用的性能,我们在Hibernate中使用了分布式缓存:OSCache。
Oscache是得到了广泛使用的开源 Cache 实现(Hibernate中对它提供了支持),它基于更加可靠高效的设计,最重要的是,新版本的OSCache已经支持集群分布式。如果系统需要在部署在集群中,或者需要部署在多机负载均衡模式的环境中来获取更高性能,那么 OSCache将是不二之选。
我们知道为了实现分布式环境下消息的通知,目前两种比较流行的做法是使用:
1 JavaGroups[http: // www.jgroups.org]
2 JMS
这两种方式都在底层实现了广播发布消息。 由于JGroups可以提供可靠的广播通信.所以我们准备采用JGroups。
一、环境说明
JavaGroups Configuration
Just make sure you have jgroups-all.jar file in your classpath (for a webapp put it in WEB-INF/lib), and add the JavaGroups broadcasting listener to your oscache.properties file like this:
cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JavaGroupsBroadcastingListener
In most cases, that's it! OSCache will now broadcast any cache flush events across the LAN. The jgroups-all.jar library is not included with the binary distribution due to its size, however you can obtain it either by downloading the full OSCache distribution, or by visiting the JavaGroups website.
If you want to run more than one OSCache cluster on the same LAN, you will need to use different multicast IP addresses. This allows the caches to exist in separate multicast groups and therefore not interfere with each other. The IP to use can be specified in your oscache.properties file by the cache.cluster.multicast.ip property. The default value is 231.12.21.132, however you can use any class D IP address. Class D address fall in the range 224.0.0.0 through 239.255.255.255.
If you need more control over the multicast configuration (eg setting network timeout or time-to-live values), you can use the cache.cluster.properties configuration property. Use this instead of the cache.cluster.multicast.ip property. The default value is:
UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\
mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\
PING(timeout=2000;num_initial_members=3):\
MERGE2(min_interval=5000;max_interval=10000):\
FD_SOCK:VERIFY_SUSPECT(timeout=1500):\
pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\
UNICAST(timeout=300,600,1200,2400):\
pbcast.STABLE(desired_avg_gossip=20000):\
FRAG(frag_size=8096;down_thread=false;up_thread=false):\
pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)
See the JavaGroups site for more information. In particular, look at the documentation of Channels in the User's Guide.
三、我在应用中使用的 OSCache.properties:
# CACHE IN MEMORY
#
# If you want to disable memory caching, just uncomment this line.
#
# 不使用内存
cache.memory=false
# CACHE KEY
#
# This is the key that will be used to store the cache in the application
# and session scope.
#
# If you want to set the cache key to anything other than the default
# uncomment this line and change the cache.key
#
# cache.key=__oscache_cache
# USE HOST DOMAIN NAME IN KEY
#
# Servers for multiple host domains may wish to add host name info to
# the generation of the key. If this is true, then uncomment the
# following line.
#
# cache.use.host.domain.in.key=true
# CACHE LISTENERS
#
# These hook OSCache events and perform various actions such as logging
# cache hits and misses, or broadcasting to other cache instances across a cluster.
# See the documentation for further information.
#
# cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener, \
# com.opensymphony.oscache.extra.CacheEntryEventListenerImpl, \
# com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl, \
# com.opensymphony.oscache.extra.ScopeEventListenerImpl
# 使用 JavaGroups 来实现监听广播式缓存
cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JavaGroupsBroadcastingListener
# CACHE PERSISTENCE CLASS
#
# Specify the class to use for persistence. If you use the supplied DiskPersistenceListener,
# don't forget to supply the cache.path property to specify the location of the cache
# directory.
#
# If a persistence class is not specified, OSCache will use memory caching only.
#
# 启动硬盘持久化 Cache
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
# CACHE OVERFLOW PERSISTENCE
# Use persistent cache in overflow or not. The default value is false, which means
# the persistent cache will be used at all times for every entry. true is the recommended setting.
#
# cache.persistence.overflow.only=true
# CACHE DIRECTORY
#
# This is the directory on disk where caches will be stored by the DiskPersistenceListener.
# it will be created if it doesn't already exist. Remember that OSCache must have
# write permission to this directory.
#
# Note: for Windows machines, this needs \ to be escaped
# ie Windows:
# cache.path=c:\\myapp\\cache
# or *ix:
# 在Linux下的Cache存放路径
cache.path=/data/jsp/boss.21cn.com/bossimpls/cache/bossuud
#
# cache.path=c:\\app\\cache
# CACHE ALGORITHM
#
# Default cache algorithm to use. Note that in order to use an algorithm
# the cache size must also be specified. If the cache size is not specified,
# the cache algorithm will be Unlimited cache.
#
#使用LRU运算算法,把最少使用的Cache剔除出去
cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache
# cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache
# cache.algorithm=com.opensymphony.oscache.base.algorithm.UnlimitedCache
# THREAD BLOCKING BEHAVIOR
#
# When a request is made for a stale cache entry, it is possible that another thread is already
# in the process of rebuilding that entry. This setting specifies how OSCache handles the
# subsequent 'non-building' threads. The default behaviour (cache.blocking=false) is to serve
# the old content to subsequent threads until the cache entry has been updated. This provides
# the best performance (at the cost of serving slightly stale data). When blocking is enabled,
# threads will instead block until the new cache entry is ready to be served. Once the new entry
# is put in the cache the blocked threads will be restarted and given the new entry.
# Note that even if blocking is disabled, when there is no stale data available to be served
# threads will block until the data is added to the cache by the thread that is responsible
# for building the data.
#
# cache.blocking=false
# CACHE SIZE
#
# Default cache size in number of items. If a size is specified but not
# an algorithm, the cache algorithm used will be LRUCache.
#
#这个值设得够大了,就是Cache的容量项目个数
cache.capacity=100000000
# CACHE UNLIMITED DISK
# Use unlimited disk cache or not. The default value is false, which means
# the disk cache will be limited in size to the value specified by cache.capacity.
#
#不使用无限硬盘空间
cache.unlimited.disk=false
# JMS CLUSTER PROPERTIES
#
# Configuration properties for JMS clustering. See the clustering documentation
# for more information on these settings.
#
#cache.cluster.jms.topic.factory=java:comp/env/jms/TopicConnectionFactory
#cache.cluster.jms.topic.name=java:comp/env/jms/OSCacheTopic
#cache.cluster.jms.node.name=node1
# JAVAGROUPS CLUSTER PROPERTIES
#
# Configuration properites for the JavaGroups clustering. Only one of these
# should be specified. Default values (as shown below) will be used if niether
# property is set. See the clustering documentation and the JavaGroups project
# (www.javagroups.com) for more information on these settings.
#
cache.cluster.properties=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\
mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\
PING(timeout=2000;num_initial_members=3):\
MERGE2(min_interval=5000;max_interval=10000):\
FD_SOCK:VERIFY_SUSPECT(timeout=1500):\
pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\
UNICAST(timeout=300,600,1200,2400):\
pbcast.STABLE(desired_avg_gossip=20000):\
FRAG(frag_size=8096;down_thread=false;up_thread=false):\
pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)
cache.cluster.multicast.ip=231.12.21.132
分享到:
相关推荐
在Java Web开发中,OSCache常被用于实现Session复制和分布式缓存,尤其在基于Spring或Hibernate的应用中,OSCache扮演着关键的角色。 `oscache.tld` 文件是 Tag Library Descriptor (TLD) 文件,它是JSP 2.0规范的...
在这个"OSCache学习例子 实例"中,我们将深入探讨如何在J2EE环境中集成并使用OSCache。 首先,理解OSCache的基本概念至关重要。OSCache的核心功能是提供内存中的对象缓存,它支持两种主要的缓存模式:本地缓存和...
它使用IP组播来有效地在缓存的实例之间进行通信。它是快速提高集群式Web应用程序的性能的理想选择。 hibernate-proxool:支持Proxool连接池,性能与DBCP不相上下。 hibernate-oscache: 支持oscache的缓冲解决...
osCache的核心功能包括对象缓存、页面缓存以及分布式缓存,使得在大型Java应用系统中能够更有效地管理内存资源。** **1. 对象缓存:** osCache允许开发者将Java对象存储到内存中,以避免每次需要这些对象时都去重新...
例如,在Hibernate中启用osCache,可以实现二级缓存,进一步提高ORM操作的效率。 7. **序列化支持**:osCache支持对象的序列化,这意味着不仅可以缓存基本类型和字符串,还可以缓存复杂的对象结构。 8. **性能优化...
6. **缓存同步**: 在分布式环境中,OSCache 支持集群间的缓存同步,保证所有节点的数据一致性。 **二、OSCache 的工作原理** OSCache 基于 JVM 内存进行缓存,通过序列化和反序列化对象来存储和读取。当一个对象被...
OSCache 是一个广泛使用的缓存解决方案,它支持多种技术,如 Hibernate、JSP 和页面缓存。本篇文章将深入探讨 OSCache 的配置和使用方法。 首先,安装 OSCache 包括以下几个步骤: 1. 从官方网站...
在J2EE环境中,OSCache可以被集成到各种Web应用服务器,如Tomcat、Jetty等,或者与Spring、Hibernate等框架无缝配合。以下将详细介绍OSCache的几个关键知识点: 1. **配置OSCache**: 配置OSCache主要涉及XML配置...
- 在Web应用中,可以利用OSCache缓存JSP页面、EJB会话bean、Hibernate查询结果等,减少服务器负载。 - 在服务端,可以缓存经常访问的API响应,提升响应速度,改善用户体验。 - 结合Spring框架,可以通过AOP(面向...
在这个“hibernate3.6所有包”压缩文件中,包含了Hibernate 3.6版本的所有核心组件和依赖库。这个版本在当时是一个重要的里程碑,因为它引入了许多改进和新特性。 1. **Hibernate核心**:这是Hibernate框架的基础,...
- 例如,如果一个对象在同一事务中被多次修改,Hibernate 只会在事务结束时生成一条包含所有更改的 SQL UPDATE 语句。 ##### 二级缓存 (Second-level Cache) - **作用范围**:与 SessionFactory 对象关联。 - **...
在Java开发中,Hibernate是一个非常流行的ORM(对象关系映射)框架,它极大地简化了数据库操作。为了提高性能,Hibernate引入了缓存机制,分为一级缓存和二级缓存。本篇文章将深入探讨Hibernate缓存,并通过实例演示...
当前常见的选择有 oscache 和 jbosscache,但在关键交易系统中使用还需谨慎考虑。 #### 六、延迟加载 1. **实体延迟加载**:通过动态代理实现,只有当真正需要实体数据时才会触发数据库查询。 2. **集合延迟加载...
4. **分布式缓存**:在集群环境中使用,例如Oscache、JBoss Cache等,提高应用的可扩展性。 #### 七、延迟加载策略 1. **实体延迟加载**:通过动态代理技术实现实体的懒加载,仅在真正需要时才加载数据。 2. **...
4. **分布式缓存**:在集群环境中使用,可以提高系统的扩展性和可用性。常用的分布式缓存解决方案包括 OSCache 和 JBoss Cache。 #### 六、方法选用 1. **List/Set/Bag/Map**:在处理大量数据时,使用基于游标的...
在Hibernate中,对象有三种状态:临时状态、持久化状态和脱管状态。对象状态的转换如下: 1. **临时状态**:通过`new`关键字创建的对象处于临时状态。 2. **持久化状态**:当对象被`Session`保存后,它变为持久化...
Hibernate不仅适用于Java客户端程序,也适用于Web应用,特别是能够在EJB架构中取代CMP,担负起数据持久化的任务。 在学习Hibernate的过程中,首先会遇到模型不匹配的问题,即阻抗不匹配,这是由于Java对象模型和...