OSCache简单例子

简介:

OSCache简单例子

 

 

 

 

1.  BaseCache.java  基类

 

 

package com.yanek.demo.cache.oscache;

import java.util.Date;

import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

public class BaseCache extends GeneralCacheAdministrator {
 // 过期时间(单位为秒);
 private int refreshPeriod;

 // 关键字前缀字符;
 private String keyPrefix;

 private static final long serialVersionUID = -4397192926052141162L;

 public BaseCache(String keyPrefix, int refreshPeriod) {
  super();
  this.keyPrefix = keyPrefix;
  this.refreshPeriod = refreshPeriod;
 }

 // 添加被缓存的对象;
 public void put(String key, Object value) {
  this.putInCache(this.keyPrefix + "_" + key, value);
 }

 // 删除被缓存的对象;
 public void remove(String key) {
  this.flushEntry(this.keyPrefix + "_" + key);
 }

 // 删除所有被缓存的对象;
 public void removeAll(Date date) {
  this.flushAll(date);
 }

 public void removeAll() {
  this.flushAll();
 }

 // 获取被缓存的对象;
 public Object get(String key) throws Exception {
  try {
   return this.getFromCache(this.keyPrefix + "_" + key,
     this.refreshPeriod);
  } catch (NeedsRefreshException e) {
   this.cancelUpdate(this.keyPrefix + "_" + key);
   throw e;
  }

 }

}

 

 

2.  CacheManager.java  管理器

 

package com.yanek.demo.cache.oscache;

public class CacheManager { 
   
    private BaseCache newsCache; 
 
     
    private static CacheManager instance; 
    private static Object lock = new Object(); 
     
    public CacheManager() { 
        //这个根据配置文件来,初始BaseCache而已; 
        newsCache = new BaseCache("news",1800);      
    } 
     
    public static CacheManager getInstance(){ 
        if (instance == null){ 
            synchronized( lock ){ 
                if (instance == null){ 
                    instance = new CacheManager(); 
                } 
            } 
        } 
        return instance; 
    } 
 
    public void putNews(News news) { 
        // TODO 自动生成方法存根 
        newsCache.put(news.getId(),news); 
    } 
 
    public void removeNews(String newsID) { 
        // TODO 自动生成方法存根 
        newsCache.remove(newsID); 
    } 
 
    public News getNews(String newsID) { 
        // TODO 自动生成方法存根 
        try { 
            return (News) newsCache.get(newsID); 
        } catch (Exception e) { 
            // TODO 自动生成 catch 块 
            System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage()); 
           // News news = new News(newsID); 
           
            News news = new News(newsID,"aaa","bbb");
            this.putNews(news); 
            return news; 
        } 
    } 
 
    public void removeAllNews() { 
        // TODO 自动生成方法存根 
        newsCache.removeAll(); 
    } 
 

 

3. News.java  缓存的对象

 

package com.yanek.demo.cache.oscache;

public class News {

 private String id;

 private String title;

 private String content;

 
 public News(String id, String title, String content) {
  super();
  this.id = id;
  this.title = title;
  this.content = content;
 }

 public String getContent() {
  return content;
 }

 public void setContent(String content) {
  this.content = content;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

}

 

 

 

4. TestCache.java 测试

 

 

package com.yanek.demo.cache.oscache;

public class TestCache {

 /**
  * @param args
  */
 public static void main(String[] args) {

  
  CacheManager cm=CacheManager.getInstance();
  News n1=new News("1","111","111");
  cm.putNews(n1);
  News n1_c=cm.getNews("1");
  System.out.println("c1:"+n1_c.getContent());
  
  News n2=new News("1","111","222");
  cm.putNews(n2);
  System.out.println("c1:"+cm.getNews("1").getContent());
  
  cm.removeNews("1");
  
  System.out.println("c1:"+cm.getNews("1").getContent());
  
  BaseCache countCache = new BaseCache("count",1800);
  
  countCache.put("1100454", 10);
  countCache.put("1100455", 11);
  countCache.put("1100456", 3);
  
  try {
   Integer cachedCount = (Integer)countCache.get("1100454");

   System.out.println("cachedCount:"+cachedCount);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
  
  
 }

}

 

 

 

目录
相关文章
|
2月前
|
缓存 NoSQL Java
springboot怎么使用rides缓存方法的返回值 完整例子
通过上述步骤,我们成功地在 Spring Boot 项目中集成了 Redis 缓存,并通过注解的方式实现了方法返回值的缓存。这种方式不仅提高了系统的性能,还简化了缓存管理的复杂度。使用 Spring Boot 的缓存注解和 Redis,可以轻松地实现高效、可靠的缓存机制。
62 23
|
8月前
|
XML JSON Java
常用工具类---JSONUtil
这段内容提供了Java中将不同数据结构转换为JSON的代码示例。包括使用`JSONArray`将一维和二维数组、对象、Map及List转换为JSON字符串,并展示了如何从JSON字符串转换回Object、List以及XML到JSON的转换。
|
8月前
|
Java
常用工具类---IOUtils
该文档是关于Java中文件操作的说明,强调所有IO操作必须在`finally`块中关闭。提供了三个示例:1) 读取指定文件`test.txt`的全部内容,使用`FileReader`和`StringBuffer`;2) 追加内容到`test.txt`文件,通过`FileOutputStream`, `OutputStreamWriter`和`BufferedWriter`实现;3) 递归遍历目录下的所有文件,使用`File`类和递归函数获取每个文件的绝对路径。
|
8月前
|
存储 Java
常用工具类---StringUtil
这篇文档讨论了Java中处理超长字符串的问题。由于字符串字面量限制为65534个字符,超过此长度会导致编译错误。当从HTTP或RPC接收长字符串时,不能直接用字符串字面量接收。解决方案是使用StringBuilder动态构建字符串,通过分组处理超过限制的字符序列。示例代码展示了如何分割并重新组合超过长度限制的字符串。
|
Java 程序员 API
Java源码类-Optional类源码分析与使用
Java源码类-Optional类源码分析与使用
86 0
|
XML 存储 安全
Java源码类 - Properties类及多种读取方式
Java源码类 - Properties类及多种读取方式
187 0
java实现遍历树形菜单方法——struts.xml实现
java实现遍历树形菜单方法——struts.xml实现
|
缓存 安全 Java
SpringSecurity——HttpSecurity常用方法
SpringSecurity——HttpSecurity常用方法
1463 0
SpringSecurity——HttpSecurity常用方法
|
Java
JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】(四)
JSTL作为最基本的标签库,提供了一系列的JSP标签,实现了基本的功能:集合的遍历、数据的输出、字符串的处理、数据的格式化等等!
178 0
JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】(四)
|
XML Java 数据格式
JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】(一)
JSTL作为最基本的标签库,提供了一系列的JSP标签,实现了基本的功能:集合的遍历、数据的输出、字符串的处理、数据的格式化等等!
159 0
JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】(一)