---
title: "oscache 对象缓存"
author: "javaeyetodj"
date: "2010-12-11T13:55:33Z"
tags: []
categories: ["编程语言"]
slug: "oscache"
draft: false
lastmod: "2010-12-13T09:09:25Z"
id: 839558
url: "https://javaeyetodj.iteye.com/blog/839558"
---

转自 [http://benx.iteye.com/blog/606091](http://benx.iteye.com/blog/606091)

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

001. 一、对象缓存
002. 1、Cache操作类
003. **import** java.util.Date;
004. **import** com.opensymphony.oscache.base.NeedsRefreshException;
005. **import** com.opensymphony.oscache.general.GeneralCacheAdministrator;
006. **public** **class** BaseCache **extends** GeneralCacheAdministrator {
007. **private** **int** refreshPeriod; //过期时间(单位为秒);
008. **private** String keyPrefix; //关键字前缀字符;
009. **private** **static** **final** **long** serialVersionUID = -4397192926052141162L;
010. **public** BaseCache(String keyPrefix,**int** refreshPeriod){
011. **super**();
012. **this**.keyPrefix = keyPrefix;
013. **this**.refreshPeriod = refreshPeriod;
014. }
015. //添加被缓存的对象;
016. **public** **void** put(String key,Object value){
017. **this**.putInCache(**this**.keyPrefix+"\_"+key,value);
018. }
019. //删除被缓存的对象;
020. **public** **void** remove(String key){
021. **this**.flushEntry(**this**.keyPrefix+"\_"+key);
022. }
023. //删除所有被缓存的对象;
024. **public** **void** removeAll(Date date){
025. **this**.flushAll(date);
026. }
027. **public** **void** removeAll(){
028. **this**.flushAll();
029. }
030. //获取被缓存的对象;
031. **public** Object get(String key) **throws** Exception{
032. **try**{
033. returnthis.getFromCache(**this**.keyPrefix+"\_"+key,**this**.refreshPeriod);
034. } **catch** (NeedsRefreshException e) {
035. **this**.cancelUpdate(**this**.keyPrefix+"\_"+key);
036. **throw** e;
037. }
038. }
039. }
040. 2、Cache管理类
041. **public** **class** CacheManager {
042. **private** BaseCache newsCache;
043. **private** **static** CacheManager instance;
044. **private** **static** Object lock = **new** Object();
045. **private** CacheManager() {
046. //这个根据配置文件来，初始BaseCache而已;
047. newsCache = **new** BaseCache("news",120);
048. }
049. **public** **static** CacheManager getInstance(){
050. **if** (instance == **null**){
051. **synchronized**( lock ){
052. **if** (instance == **null**){
053. instance = **new** CacheManager();
054. }
055. }
056. }
057. **return** instance;
058. }
059. **public** **void** putUser(User news) { newsCache.put(news.getId()+"",news);     }
060. **public** **void** removeUser(String newsID) {  newsCache.remove(newsID);      }
061. **public** User getUser(**int** newsID) {
062. **try** {
063. **return** (User) newsCache.get(newsID+"");
064. } **catch** (Exception e) {
065. System.out.println("getNews&gt;&gt;newsID\["+newsID+"]&gt;&gt;"+e.getMessage());
066. User news = **new** User(newsID);
067. **this**.putUser(news);
068. **return** news;
069. }
070. }
071. **public** **void** removeAllNews() {
072. newsCache.removeAll();
073. }
074. }
075. 3、对象Bean
076. **public** **class** User {
077. **private** **int** id;
078. **private** String name;
079. **private** String sex;
080. **private** **int** age;
081. **private** Date accessTime; **public** User(**int** id) {
082. **super**();
083. **this**.id = id;
084. **this**.accessTime = **new** Date(System.currentTimeMillis());
085. }
086. **public** String toString() {
087. **return** "User info is : id=" + id + "  accessTime="
088. + accessTime.toString();
089. }
090. **public** User(String name, String sex, **int** age) {
091. **super**();
092. **this**.name = name;
093. **this**.sex = sex;
094. **this**.age = age;
095. }
096. **public** User() {
097. }
098. **public** **int** getAge() {
099. **return** age;
100. }
101. **public** **void** setAge(**int** age) {
102. **this**.age = age;
103. }
104. **public** String getName() {
105. **return** name;
106. }
107. **public** **void** setName(String name) {
108. **this**.name = name;
109. }
110. **public** String getSex() {
111. **return** sex;
112. }
113. **public** **void** setSex(String sex) {
114. **this**.sex = sex;
115. }
116. **public** **int** getId() {
117. **return** id;
118. }
119. **public** **void** setId(**int** id) {
120. **this**.id = id;
121. }
122. **public** Date getAccessTime() {
123. **return** accessTime;
124. }
125. **public** **void** setAccessTime(Date accessTime) {
126. **this**.accessTime = accessTime;
127. }
128. }
129. 4、测试类
130. **public** **class** TestObjectCache {
131. **public** **static** **void** main(String\[] args) {
132. CacheManager cm=CacheManager.getInstance();
133. TestObjectCache test=**new** TestObjectCache();
134. test.print(cm);
135. }
136. **public** **void** print(CacheManager cm){
137. User user=**null**;
138. **for** (**int** i = 0; i &lt; 1000; i++) {
139. user=cm.getUser(100);
140. System.out.println("&lt;&lt;"+i+"&gt;&gt;: "+user);
141. **if**(i==10){
142. //删除缓存id的对象
143. cm.removeUser(100+"");
144. }
145. **if**(i==20){
146. //删除所有缓存的对象
147. cm.removeAllNews();
148. }
149. // 睡眠部分
150. **try** {
151. Thread.sleep(30000);
152. } **catch** (Exception e) {
153. }
154. }
155. }
156. }