`
jackmisweijie
  • 浏览: 71998 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类
最新评论

使用OSCache进行简单的缓存

 
阅读更多
使用OSCache进行简单的缓存
      在目前流行的三种开源的缓存工具中,OSCache的配置和使用应给是最简单的了,它主要是针对页面级的配置,EHCache主要针对对象级的缓存,MemCached应该是比较完整的了。接下来我们简单的讲一下在你的系统中怎样快速的应用上OSCache。只需简单的两步。
     第一步:加载oscache.properties文件,默认放到src目录下。下面是oscache.xml的默认配置。你只需简单的去掉一些注释就可以用了。
# 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,                    \
#                       com.opensymphony.oscache.extra.StatisticListenerImpl

# 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.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
# cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener
# 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:
# cache.path=/opt/myapp/cache
#
# 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.
#
# 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.capacity=1000

# 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

第二步:在web.xml进行一些简单的配置,oscache提供了强大的标签。主要是针对jsp的,如果项目中需把oscache.tld放到WEB-INF/classes下,在再web.xml中配置一下。

<!--oscache taglib  -->
<jsp-config>
  <taglib>
   <taglib-uri>oscache</taglib-uri>
   <taglib-location>
    /WEB-INF/classes/oscache.tld
   </taglib-location>
  </taglib>
</jsp-config>
在jsp中应用的话,还需简单的引入。<%@ taglib uri="oscache" prefix="cache" %>
具体的应用:<cache:cache key="testcache"></cache:cache>。
    以上是针对具体的页面的配置步骤。如果想针对某一页面的类型进行配置,可以利用过滤器来配置。比如我们想对以*.ftl和*.jsp的页面进行缓存的话,我们只需这样简单的配置在web.xml中。
<!-- OScacheFilter  -->
<filter>
  <filter-name>CacheFilter</filter-name>
  <filter-class>
   com.opensymphony.oscache.web.filter.CacheFilter
  </filter-class>
  <init-param>
   <param-name>time</param-name>
   <param-value>3600</param-value>
  </init-param>
  <init-param>
   <param-name>scope</param-name>
   <param-value>application</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CacheFilter</filter-name>
  <url-pattern>*.ftl</url-pattern>
</filter-mapping>
<filter-mapping>
  <filter-name>CacheFilter</filter-name>
  <url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!-- OScacheFilter  -->

如果想对某一访问的路径action进行缓存的话,我们可以这样做,在一个jsp文件中简单的加上:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="oscache" prefix="cache" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>My JSP 'index.jsp' starting page</title>
</head>
<body>
  <oscache:cache>
   <jsp:include page ="/view.do"/>
  </oscache:cache>
</body>
</html>
       OSCache的配置比较活的,你可以根据你的情况进行相应的配置。它主要是针对页面级的对象。虽然它专门为jsp提供了强大的标签支持,并不意味着不能对ftl,php进行缓存的,我们可以用jsp中的标签来嵌套我们的返回不同的数据页面。比如上面的/view.do可以返回的是一个ftl页面,照样可以缓存。
       要想对缓存有更深入的了解,可以查看源码,可能会帮助你更好的理解它的工作机制和原理。
       简单的说,缓存就是Map<key,value>,创建缓存就是添加一个map,使用就是通过key取value.
        写的有不当之处,敬请提出,以待改正。避免初学者误解。Thank you.
        
分享到:
评论

相关推荐

    基于OSCache的页面缓存(收藏)

    这篇博客将深入探讨如何利用OSCache来实现页面缓存,并结合实际案例进行解析。 首先,我们需要理解页面缓存的基本原理。页面缓存是指将频繁访问的网页内容存储在内存中,当用户请求这些页面时,不再需要重新生成...

    oscache-java缓存框架

    - **API集成**:使用osCache提供的API进行缓存的增删改查操作,如`CacheManager.getInstance().getCache("myCache")`获取缓存实例。 4. **使用教程** - **创建缓存**:定义一个缓存,例如`Cache cache = ...

    oscache-JSP缓存

    - 编写Java代码:在Servlet或Controller中,使用osCache API进行缓存操作。 **5. 示例应用** 以下是一个简单的osCache在JSP页面中的应用示例: ```jsp &lt;%@ taglib uri="http://www.opensymphony.com/oscache" ...

    用OSCache进行缓存对象

    1、OSCache是什么? 2、OSCache的特点 3、有关“用OSCache进行缓存对象”的研究

    OsCache缓存框架使用示例

    OsCache提供了put方法进行存储,并可以设置过期时间。 ```java OsCache weatherCache = OsCacheManager.getInstance("weatherCache"); weatherCache.put(cityId, weatherInfo, expireTimeInSeconds); ``` 5. 获取...

    oscache,缓存机制的使用

    标题与描述概述的知识点主要集中在oscache的使用及其在Java环境下的配置与实施。oscache是一种缓存机制,主要用于提高Web应用的响应速度和优化系统性能。以下是对这些知识点的详细解析: ### oscache简介 oscache...

    oscache缓存技术

    **osCache缓存技术详解** osCache是一款广泛应用于Java应用程序中的开源缓存解决方案,由OpenSymphony团队开发。它提供了一种高效、可扩展的方式来管理应用程序中的数据缓存,从而提高系统的性能和响应速度。...

    二级缓存OScache配置

    OSCache的灵活性在于能够适应不同需求和环境,支持对部分页面内容或页面级响应内容进行缓存,开发者可以根据实际情况选择合适的缓存策略。 **五、容错机制** 在遇到数据库不可用等情况时,OSCache可以提供缓存内容...

    基于OSCache的页面缓存

    OSCache组件提供的`CacheFilter`是一个关键元素,它能对整个动态页面进行缓存。通过在`web.xml`配置文件中添加相应的过滤器配置,我们可以指定哪些页面需要被缓存。例如,下面的配置会将所有`.jsp`页面进行缓存,...

    OSCache缓存框架的简单用法

    此外,OSCache还提供定时任务进行缓存清理。 **8. 监控与调试** OSCache提供了一套监控工具,可以帮助开发者查看缓存状态、性能指标等信息。同时,它还支持日志记录,便于调试和问题定位。 总之,OSCache作为一个...

    oscache对象缓存

    oscache不仅限于简单的对象缓存,还提供了集群缓存的支持。在分布式环境中,多台服务器共享同一份缓存,确保所有节点都能访问到最新的数据。通过使用JMS(Java Message Service)或RMI(Remote Method Invocation)...

    oscache缓存技术入门实例

    在本文中,我们将深入探讨osCache的基本概念、工作原理以及如何在实际项目中进行配置和使用。 一、osCache基本概念 osCache的核心理念是将数据存储在内存中,以减少对数据库或其他资源的频繁访问,从而提高系统的...

    oscache缓存配置

    《osCache缓存配置详解》 osCache是Java平台上的一个高效、易用的缓存解决方案,它由Tuckey组织开发,广泛应用于各种Web应用中,以提高数据读取速度,减轻数据库压力。osCache的核心功能是提供了一个内存中的对象...

    oscache缓存使用总结.doc

    OSCache的使用包括在JSP或Servlet中使用OSCache标签,例如`&lt;oscache:cache&gt;`,或者通过API进行编程式缓存操作。可以通过设置key和expiration时间来缓存特定内容,当数据变化时,可以自动更新缓存。 在实际应用中,...

    osCache,缓存

    3. 编写缓存逻辑:使用osCache提供的API进行缓存的插入、查询、更新和删除操作。 4. 集成到应用程序:根据应用场景,将osCache集成到业务代码中,如在数据库查询前后添加缓存处理。 5. 测试与优化:测试缓存效果,...

    OSCache缓存技术(6)【实例】

    以上就是OSCache缓存技术的基本介绍,通过理解并熟练掌握OSCache,开发者能够有效地提升应用性能,降低数据库压力,为用户带来更流畅的体验。在实践中,还需要根据具体项目需求进行优化和调整,以达到最佳的缓存效果...

    oscache缓存

    首先,osCache的内存管理机制允许开发者设置缓存的最大大小,一旦达到设定值,它会根据预设的淘汰策略自动清理过期或最少使用的项。这有助于防止内存溢出,并保持系统稳定运行。 其次,osCache提供了多种缓存策略。...

    hibernate+oscache实现二级缓存实例

    为了提高系统性能,通常会采用缓存技术来减少对数据库的直接访问,而OSCache就是一种广泛使用的开源缓存解决方案。本实例将介绍如何结合Hibernate和OSCache实现二级缓存,以优化应用程序的性能。 一级缓存是...

    oscache的使用实例和详解

    osCache的实例应用通常会结合实际业务需求进行调整,例如设置适当的过期时间、选择合适的缓存同步策略等。 总的来说,osCache是一个功能强大、易于使用的Java缓存框架,对于优化应用性能、减轻数据库压力具有重要...

Global site tag (gtag.js) - Google Analytics