项目中用到OSCache来实现jsp页面缓存功能的杂谈

06-01-12 python
本人是一新手,项目中用到OSCache来实现jsp页面缓存功能,自己大概总结了一下,有不足之处,请大家别见笑!
1.jsp页面中定义。
<%@ taglib uri="oscache" prefix="cache" %>
<cache:cache key="关键字" time="300" scope="application">
...要缓存的部分...
</cache:cache>
2.web.xml中定义。
<taglib>
<taglib-uri>oscache</taglib-uri>
<taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
</taglib>
3.oscache.tld
......
<name>cache</name>
<tag-class>com.opensymphony.oscache.web.tag.CacheTag</tag-class>
<body-content>JSP</body-content>
........
4.用的是oscache-2.1.jar
放在你的应用服务器lib\目录下或你的应用程序\WEB-INF\lib\下
注意的地方:
有cache的地方,session就不能用了。
搜索页面部分不要用cache,每个人搜索的内容都不一样的。
关键子的定义,每个页面的关键子当然都不一样的,另外如果有分页,得把分页参数取得,放入关键字里,(不然的话,每页内容都一样了),如果还有其他的传入参数,比如查看不同分类,会有不同分类的ID,也要放到关键字里。
执行一次JSP,而后在设定的时间内,客户端访问的都是HTML页面。

小弟是一新手,大家见笑了!

python
2006-01-12 17:09
1.jsp页面中定义。
《%@ taglib uri="oscache" prefix="cache" %》
《cache:cache key="关键字" time="300" scope="application"》
...要缓存的部分...
《/cache:cache》
2.web.xml中定义。
《taglib》
《taglib-uri》oscache《/taglib-uri》
《taglib-location》/WEB-INF/classes/oscache.tld《/taglib-location》
《/taglib》
3.oscache.tld
......
《name》cache《/nam》
《tag-class》com.opensymphony.oscache.web.tag.CacheTag《/tag-class》
《body-content》JSP《/body-content》
........

banq
2006-01-12 17:13
非常不错,贴代码时,选择上方的Code即可。
在web.xml中,配置OScache拦截时,好像只能是*.jsp,如果是*就出错。

python
2006-01-12 17:17
好兴奋,感谢斑竹的肯定,以后我会把工作中的经验发到这里,和大家一起分享!大家共同进步!

cute
2006-01-13 14:24
我在一个门户网站中就全面用到了OSCACHE缓存系统,我感觉还是不错的。

搂住说的功能,是实现内存缓存方式。另外一种就是磁盘缓存,可以在OSCACHE配置文件中,指定磁盘物理目录,系统运行后,就会把缓存的页面,全部以文件形式存储在目录中。后者一般是针对内存不足以够的情况下使用。

缓存的数据每30分钟自动刷新数据,这是默认的。可以在WEB。XML中指定刷新时间,也可以手工刷新。


另外,OSCache还支持集群功能.



磁盘缓存实现:

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


# 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.HashDiskPersistenceListener
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=false

# 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=F:\\jboss-3.2.1\\server\\default\\deploy\\weihai.war\\powercache


# 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=true


# 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=4000



# CACHE UNLIMITED DISK ??????????????????1000??????????1000???LRU?????????????????
# 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=true



# 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
<p class="indent">

cute
2006-01-13 14:27
如果企业有钱的话,就不推荐使用OSCache....
他们可以去买网站内容管理,这样就可以生成静态页面了.

.OSCache无非就是减轻服务器压力....




ttzx8888
2006-02-03 01:39
请问 OSCACHE 中 缓存的作用范围
APPLICATION SESSION
到底该怎么选择 是什么意思

zrweng
2006-02-03 11:19
> 请问 OSCACHE 中 缓存的作用范围
> APPLICATION SESSION
> 到底该怎么选择 是什么意思

这样看情况了,如果是Portal系统,大多数页面对于大多数人来说是一样的,那么使用Application,如果是MIS系统,将涉及数据权限,用JSP缓存的意义并不大,一般是采用对象缓存.

ttzx8888
2006-02-03 12:23
再请问下
OSCACHE对 JSP部分内容的缓存

<cache:cache>
<%
//自己的JSP代码内容
%>
</cache:cache>

在<cache:cache> </cache:cache>标签之间是否可包含其他的标签
比如STRUTS的

ttzx8888
2006-02-03 12:25
再请问下
OSCACHE对 JSP部分内容的缓存


<cache:cache key="foobar" scope="session">
<%
//自己的JSP代码内容
%>
</cache:cache> 



在<cache:cache></cache:cache> 标签之间是否可包含其他的标签
比如STRUTS的

conjee
2006-03-08 09:26
如果我的应用是采用STRUTS模式,几乎所有的JSP都是通过.do的方式跳转,那么用OSCACHE是否有效,我考虑在ACTION里来CACHE,但是研究了一天,对一般对象可以,但是如果是采用类的反射机制取得的LIST,putincache和getfromcache的值是空,不知道什么原因,大家可以研究一下

bigdogyang
2006-03-23 11:49
小弟现在做一个项目,想用OS Cache做业务层的部分数据的缓存,不知道用OS Cache能不能做,具体的思路是什么样的,麻烦高人指点一二。

xyxx
2006-03-23 17:52
从功能来讲,oscache是个可用的缓存。
从性能来讲,oscache缓存的性能很差

sim114
2006-03-24 12:16

从性能来讲,oscache缓存的性能很差
<p class="indent">


性能很差?那方面?

sim114
2006-03-24 13:18
内存使用太大? 命中率太低?