• 用OSCache进行缓存对象


        1. package com.klstudio.cache;  

    2.

    3. import java.util.Date;

    4.

    5. import com.opensymphony.oscache.base.NeedsRefreshException;

    6. import com.opensymphony.oscache.general.GeneralCacheAdministrator;

    7.

    8. public class BaseCache extends GeneralCacheAdministrator {

    9. //过期时间(单位为秒);

    10. private int refreshPeriod;

    11. //关键字前缀字符;

    12. private String keyPrefix;

    13.

    14. private static final long serialVersionUID = -4397192926052141162L;

    15.

    16. public BaseCache(String keyPrefix,int refreshPeriod){

    17. super();

    18. this.keyPrefix = keyPrefix;

    19. this.refreshPeriod = refreshPeriod;

    20. }

    21. //添加被缓存的对象;

    22. public void put(String key,Object value){

    23. this.putInCache(this.keyPrefix+"_"+key,value);

    24. }

    25. //删除被缓存的对象;

    26. public void remove(String key){

    27. this.flushEntry(this.keyPrefix+"_"+key);

    28. }

    29. //删除所有被缓存的对象;

    30. public void removeAll(Date date){

    31. this.flushAll(date);

    32. }

    33.

    34. public void removeAll(){

    35. this.flushAll();

    36. }

    37. //获取被缓存的对象;

    38. public Object get(String key) throws Exception{

    39. try{

    40. return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);

    41. } catch (NeedsRefreshException e) {

    42. this.cancelUpdate(this.keyPrefix+"_"+key);

    43. throw e;

    44. }

    45.

    46. }

    47.

    48. }



    view plainprint?

    1. package com.klstudio;

    2.

    3. import com.klstudio.News;

    4. import com.klstudio.cache.BaseCache;

    5.

    6. public class CacheManager {

    7.

    8. private BaseCache newsCache;

    9.

    10.

    11. private static CacheManager instance;

    12. private static Object lock = new Object();

    13.

    14. public CacheManager() {

    15. //这个根据配置文件来,初始BaseCache而已;

    16. newsCache = new BaseCache("news",1800);

    17. }

    18.

    19. public static CacheManager getInstance(){

    20. if (instance == null){

    21. synchronized( lock ){

    22. if (instance == null){

    23. instance = new CacheManager();

    24. }

    25. }

    26. }

    27. return instance;

    28. }

    29.

    30. public void putNews(News news) {

    31. // TODO 自动生成方法存根

    32. newsCache.put(news.getID(),news);

    33. }

    34.

    35. public void removeNews(String newsID) {

    36. // TODO 自动生成方法存根

    37. newsCache.remove(newsID);

    38. }

    39.

    40. public News getNews(String newsID) {

    41. // TODO 自动生成方法存根

    42. try {

    43. return (News) newsCache.get(newsID);

    44. } catch (Exception e) {

    45. // TODO 自动生成 catch 块

    46. System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());

    47. News news = new News(newsID);

    48. this.putNews(news);

    49. return news;

    50. }

    51. }

    52.

    53. public void removeAllNews() {

    54. // TODO 自动生成方法存根

    55. newsCache.removeAll();

    56. }

    57.

    58. }
    成长的乐趣,在于分享!
    大龄程序员,一路走来,感慨颇多。闲暇时写写字,希望能给同行人一点帮助。
    本文版权归作者growithus和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    mybatis-generator自动生成代码时,只生成insert方法
    elasticsearch-head-master下运行npm install报npm WARN elasticsearch-head@0.0.0 license should be a valid SPDX license expression
    fs.default.name和fs.defaultFS
    zookeeper集群为什么要是单数
    Quorom机制
    把数据库放入Docker是一个好主意吗?
    JVM GC算法CMS详解
    JVM之——CMS
    对于TCP/IP协议的三次握手和四次挥手的理解
    JVM G1和CMS
  • 原文地址:https://www.cnblogs.com/growithus/p/11012520.html
Copyright © 2020-2023  润新知