java缓存对象实例代码_Java缓存池代码实例详解

这篇实例中有四个类,分别为

CacheItem 缓存实体类

CachePool 缓存池

Student 学生实体类

MainTest 主测试类

其中,缓存实体类CacheItem 中存放管理学生实体对象Student ,缓存实体类CacheItem 存放在缓存池CachePool 中,MainTest 主要负责整体的测试工作。

缓存实体类

package com.paic.zhangqi.cache;

import java.util.Date;

/**

* 缓存实体

* @author ZHANGQI947

*/

public class CacheItem {

// 创建缓存时间

private Date createTime = new Date();

// 缓存期满时间

private long expireTime = 1;

// 缓存实体

private Object entity;

public CacheItem(Object obj, long expires) {

this.entity = obj;

this.expireTime = expires;

}

// 判断缓存是否超时

public boolean isExpired() {

return (expireTime != -1 && new Date().getTime() - createTime.getTime() > expireTime);

}

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

public Object getEntity() {

return entity;

}

public void setEntity(Object entity) {

this.entity = entity;

}

public long getExpireTime() {

return expireTime;

}

public void setExpireTime(long expireTime) {

this.expireTime = expireTime;

}

}

缓存池CachePool

package com.paic.zhangqi.cache;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

/**

* 缓存池

* @author Administrator

*/

public class CachePool {

// 缓存池唯一实例

private static CachePool instance;

// 缓存Map

private static Map cacheItems;

private CachePool() {

cacheItems = new HashMap();

}

/**

* 获取唯一的实例

* @return instance

*/

public synchronized static CachePool getInstance() {

if (instance == null) {

instance = new CachePool();

}

return instance;

}

/**

* 清除所有的Item缓存

*/

public synchronized void clearAllItems() {

cacheItems.clear();

}

/**

* 获取缓存实例

* @param name 缓存名称

* @return 缓存实例

*/

public synchronized Object getCacheItem(String name) {

if (!cacheItems.containsKey(name)) {

return null;

}

CacheItem cacheItem = (CacheItem) cacheItems.get(name);

if (cacheItem.isExpired()) {

return null;

}

return cacheItem.getEntity();

}

/**

* 存放缓存信息

* @param name 名称

* @param obj 实例对象

* @param expires 超时时长

*/

public synchronized void putCacheItem(String name, Object obj, long expires) {

// 判断该对象是否在在缓存池,不在直接put

if (!cacheItems.containsKey(name)) {

cacheItems.put(name, new CacheItem(obj, expires));

}

// 获取缓存池中对象,更新对象信息

CacheItem cacheItem = (CacheItem) cacheItems.get(name);

cacheItem.setCreateTime(new Date());

cacheItem.setEntity(obj);

cacheItem.setExpireTime(expires);

}

/**

* 移除缓存数据

* @param name

*/

public synchronized void removeCacheItem(String name) {

if (!cacheItems.containsKey(name)) {

return ;

}

cacheItems.remove(name);

}

/**

* 获取缓存数据的数量

* @return

*/

public int getSize() {

return cacheItems.size();

}

}

学生类Student

package com.paic.zhangqi.cache;

/**

* 学生类

* @author Administrator

*/

public class Student {

private String name;

private String id;

private int age;

private int sal;

public Student() {

}

public Student(String name, String id, int age, int sal) {

this.name = name;

this.id = id;

this.age = age;

this.sal = sal;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public int getSal() {

return sal;

}

public void setSal(int sal) {

this.sal = sal;

}

}

主测试类MainTest

package com.paic.zhangqi.cache;

/**

* 主测试类

* @author ZHANGQI947

*/

public class MainTest {

/**

* @param args

* @throws InterruptedException

*/

public static void main(String[] args) throws InterruptedException {

// 获取缓存池

CachePool cachePool = CachePool.getInstance();

Student stu1 = new Student("l1", "stu001", 25, 40);

Student stu2 = new Student("l2", "stu002", 25, 40);

Student stu3 = new Student("l3", "stu003", 25, 40);

Student stu4 = new Student("l4", "stu004", 25, 40);

cachePool.putCacheItem("001", stu1, 122222);

cachePool.putCacheItem("002", stu2, 10);

cachePool.putCacheItem("003", stu3, 360002);

cachePool.putCacheItem("004", stu4, 1222222);

// 设置线程休眠,其中002对象会超时

Thread.sleep(200);

Student stu001 = (Student) cachePool.getCacheItem("001");

if (null != stu001) {

System.out.println(stu001.getName());

}

// 由于超时,这里取出的002对象为null

Student stu002 = (Student) cachePool.getCacheItem("002");

if (null != stu002) {

System.out.println(stu002.getName());

}

// 获取打印缓存池中对象数量

int cacheSize = cachePool.getSize();

System.out.println(cacheSize);

// 删除对象002

cachePool.removeCacheItem("002");

// 打印缓存池数量

cacheSize = cachePool.getSize();

System.out.println(cacheSize);

}

}

测试结果

l1

4

3

希望本篇文章内容对您有所帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值