ITeye博客

oscache 对象缓存

javaeyetodj · 2010-12-11 13:55 · 缓存 · 编程语言 · Markdown

转自 http://benx.iteye.com/blog/606091

Java代码 复制代码

  1. 一、对象缓存
  2. 1、Cache操作类
  3. import java.util.Date;
  4. import com.opensymphony.oscache.base.NeedsRefreshException;
  5. import com.opensymphony.oscache.general.GeneralCacheAdministrator;
  6. public class BaseCache extends GeneralCacheAdministrator {
  7. private int refreshPeriod; //过期时间(单位为秒);
  8. private String keyPrefix; //关键字前缀字符;
  9. private static final long serialVersionUID = -4397192926052141162L;
  10. public BaseCache(String keyPrefix,int refreshPeriod){
  11. super();
  12. this.keyPrefix = keyPrefix;
  13. this.refreshPeriod = refreshPeriod;
  14. }
  15. //添加被缓存的对象;
  16. public void put(String key,Object value){
  17. this.putInCache(this.keyPrefix+"_"+key,value);
  18. }
  19. //删除被缓存的对象;
  20. public void remove(String key){
  21. this.flushEntry(this.keyPrefix+"_"+key);
  22. }
  23. //删除所有被缓存的对象;
  24. public void removeAll(Date date){
  25. this.flushAll(date);
  26. }
  27. public void removeAll(){
  28. this.flushAll();
  29. }
  30. //获取被缓存的对象;
  31. public Object get(String key) throws Exception{
  32. try{
  33. returnthis.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);
  34. } catch (NeedsRefreshException e) {
  35. this.cancelUpdate(this.keyPrefix+"_"+key);
  36. throw e;
  37. }
  38. }
  39. }
  40. 2、Cache管理类
  41. public class CacheManager {
  42. private BaseCache newsCache;
  43. private static CacheManager instance;
  44. private static Object lock = new Object();
  45. private CacheManager() {
  46. //这个根据配置文件来,初始BaseCache而已;
  47. newsCache = new BaseCache("news",120);
  48. }
  49. public static CacheManager getInstance(){
  50. if (instance == null){
  51. synchronized( lock ){
  52. if (instance == null){
  53. instance = new CacheManager();
  54. }
  55. }
  56. }
  57. return instance;
  58. }
  59. public void putUser(User news) { newsCache.put(news.getId()+"",news);     }
  60. public void removeUser(String newsID) {  newsCache.remove(newsID);      }
  61. public User getUser(int newsID) {
  62. try {
  63. return (User) newsCache.get(newsID+"");
  64. } catch (Exception e) {
  65. System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());
  66. User news = new User(newsID);
  67. this.putUser(news);
  68. return news;
  69. }
  70. }
  71. public void removeAllNews() {
  72. newsCache.removeAll();
  73. }
  74. }
  75. 3、对象Bean
  76. public class User {
  77. private int id;
  78. private String name;
  79. private String sex;
  80. private int age;
  81. private Date accessTime; public User(int id) {
  82. super();
  83. this.id = id;
  84. this.accessTime = new Date(System.currentTimeMillis());
  85. }
  86. public String toString() {
  87. return "User info is : id=" + id + "  accessTime="
  88. + accessTime.toString();
  89. }
  90. public User(String name, String sex, int age) {
  91. super();
  92. this.name = name;
  93. this.sex = sex;
  94. this.age = age;
  95. }
  96. public User() {
  97. }
  98. public int getAge() {
  99. 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 < 1000; i++) {
  139. user=cm.getUser(100);
  140. System.out.println("<<"+i+">>: "+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. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。