Java实现简单的缓存机制原理

Python010

Java实现简单的缓存机制原理,第1张

package qinbo hui

/*

设计思想来自-回钦波(qq: )

*/

public class CacheConfModel implements java io Serializable{

private long beginTime

private boolean isForever = false

private int durableTime

public long getBeginTime() {

return beginTime

}

public void setBeginTime(long beginTime) {

this beginTime = beginTime

}

public boolean isForever() {

return isForever

}

public void setForever(boolean isForever) {

this isForever = isForever

}

public int getDurableTime() {

return durableTime

}

public void setDurableTime(int durableTime) {

this durableTime = durableTime

}

}

package qinbo hui

import java util *

import test CacheConfModel

/*

设计思想来自-回钦波(qq: )

*/

public class CacheMgr {

private static Map cacheMap = new HashMap()

private static Map cacheConfMap = new HashMap()

private CacheMgr(){

}

private static CacheMgr cm = null

public static CacheMgr getInstance(){

if(cm==null){

cm = new CacheMgr()

Thread t = new ClearCache()

t start()

}

return cm

}

/**

* 增加缓存

* @param key

* @param value

* @param ccm 缓存对象

* @return

*/

public  boolean addCache(Object key Object value CacheConfModel ccm){

boolean flag = false

cacheMap put(key value)

cacheConfMap put(key ccm)

System out println( now addcache== +cacheMap size())

return true

}

/**

* 删除缓存

* @param key

* @return

*/

public  boolean removeCache(Object key){

cacheMap remove(key)

cacheConfMap remove(key)

System out println( now removeCache== +cacheMap size())

return true

}

/**

* 清除缓存的类

* @author wanglj

* 继承Thread线程类

*/

private static class ClearCache extends Thread{

public void run(){

while(true){

Set tempSet = new HashSet()

Set set = cacheConfMap keySet()

Iterator it = erator()

while(it hasNext()){

Object key = it next()

CacheConfModel ccm = (CacheConfModel)cacheConfMap get(key)

//比较是否需要清除

if(!ccm isForever()){

if((new Date() getTime() ccm getBeginTime())>= ccm getDurableTime()* * ){

//可以清除 先记录下来

tempSet add(key)

}

}

}

//真正清除

Iterator tempIt = erator()

while(tempIt hasNext()){

Object key = tempIt next()

cacheMap remove(key)

cacheConfMap remove(key)

}

System out println( now thread================>+cacheMap size())

//休息

try {

Thread sleep( * L)

} catch (InterruptedException e) {

// TODO Auto generated catch block

e printStackTrace()

}

}

}

}

lishixinzhi/Article/program/Java/hx/201311/25737

read方法又不止一个……

public int read(byte[] b)

throws IOException

从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b

中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。

如果 b 的长度为 0,则不读取任何字节并返回

0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值

-1;否则,至少读取一个字节并将其存储在 b 中。