Java cache pool code example in detail
- 2020-07-21 07:43:56
- OfStack
There are four classes in this example, respectively
CacheItem caches entity classes
CachePool buffer pool
Student Student Entity class
MainTest main test class
Among them, the cache entity class CacheItem stores the student entity object Student, the cache entity class CacheItem stores the cache pool CachePool, and MainTest is mainly responsible for the overall testing work.
Cache entity class
package com.paic.zhangqi.cache;
import java.util.Date;
/**
* Cached entities
* @author ZHANGQI947
*/
public class CacheItem {
// Create cache time
private Date createTime = new Date();
// Cache expiration time
private long expireTime = 1;
// Cached entities
private Object entity;
public CacheItem(Object obj, long expires) {
this.entity = obj;
this.expireTime = expires;
}
// Determines whether the cache has timed out
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;
}
}
Buffer pool CachePool
package com.paic.zhangqi.cache;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Buffer pool
* @author Administrator
*/
public class CachePool {
// Buffer pool only 1 The instance
private static CachePool instance;
// The cache Map
private static Map<String, Object> cacheItems;
private CachePool() {
cacheItems = new HashMap<String, Object>();
}
/**
* Get only 1 An instance of the
* @return instance
*/
public synchronized static CachePool getInstance() {
if (instance == null) {
instance = new CachePool();
}
return instance;
}
/**
* Clear all Item The cache
*/
public synchronized void clearAllItems() {
cacheItems.clear();
}
/**
* Get cache instance
* @param name Name of the cache
* @return Cache instance
*/
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();
}
/**
* Storing cache information
* @param name The name of the
* @param obj Instance objects
* @param expires Timeout value
*/
public synchronized void putCacheItem(String name, Object obj, long expires) {
// Determines whether the object is in the cache pool, not directly put
if (!cacheItems.containsKey(name)) {
cacheItems.put(name, new CacheItem(obj, expires));
}
// Gets the object in the cache pool and updates the object information
CacheItem cacheItem = (CacheItem) cacheItems.get(name);
cacheItem.setCreateTime(new Date());
cacheItem.setEntity(obj);
cacheItem.setExpireTime(expires);
}
/**
* Remove cached data
* @param name
*/
public synchronized void removeCacheItem(String name) {
if (!cacheItems.containsKey(name)) {
return ;
}
cacheItems.remove(name);
}
/**
* Gets the amount of cached data
* @return
*/
public int getSize() {
return cacheItems.size();
}
}
The student class Student
package com.paic.zhangqi.cache;
/**
* Students in class
* @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;
}
}
The main test class MainTest
package com.paic.zhangqi.cache;
/**
* The main test class
* @author ZHANGQI947
*/
public class MainTest {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// Get the cache pool
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);
// Set the thread to sleep, where 002 Object will time out
Thread.sleep(200);
Student stu001 = (Student) cachePool.getCacheItem("001");
if (null != stu001) {
System.out.println(stu001.getName());
}
// Because of the timeout, this is taken out 002 The object of null
Student stu002 = (Student) cachePool.getCacheItem("002");
if (null != stu002) {
System.out.println(stu002.getName());
}
// Gets the number of objects in the print cache pool
int cacheSize = cachePool.getSize();
System.out.println(cacheSize);
// Delete the object 002
cachePool.removeCacheItem("002");
// Print the number of cache pools
cacheSize = cachePool.getSize();
System.out.println(cacheSize);
}
}
The test results
l1
4
3
I hope this article has been helpful to you