您的位置:首页 > 数据库

数据库缓存算法思想与实现

2007-07-05 12:32 567 查看
这个算法我花一整整一天才做出来,真的好欣慰,现在写一个简单的示例程序,看的懂就看,看不懂回家再练练。

当然了,真正的应用程序肯定没有这么简单,不过我写出来了也没有人能看得懂,因为包含我的N多个业务方法,我想一周之后,我自己都看不懂了,呵呵。

数据库缓存算法:
适用条件:
1,数据库记录访问极度频繁,但更新很少。
2,数据库记录很大,但所需数据却很少。

编程思想:
由两个线程来处理
线程1:每N小时扫描一次数据库,把符合条件的数据取出,放入缓存
线程2:实时扫描缓存对像,取出符合条件的记录处理。

实现程序代码:

public class PipedStreamDemo {

/**
* @author 黄越勇
* @create date 2007-6-27
*/
public static void main(String[] args) {

//主方法,不想多说了。

PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
try {
pos.connect(pis);
new Producer(pos).start();
new Customer(pis).start();
} catch (IOException e) {
e.printStackTrace();
}

}

}

/******************************************************************************
* ****************************************************************************
* ******** 生产者线程,修改以下代码,把从数据库里取出来的数据以对象的形式存起来。
* ****************************************************************************
*/

class Producer extends Thread {
private PipedOutputStream pos;

public Producer(PipedOutputStream pos) {
this.pos = pos;
}

public synchronized void run() {
try {

/* 将对象写到一个文件里 这个很重要,相当于缓存*/
File f=new File("myvector.obj");
if(!f.exists())f.createNewFile();
FileOutputStream objfile = new FileOutputStream(f);
/* 创建一个输出流 */
ObjectOutputStream p = new ObjectOutputStream(objfile);
/* 创建一个TestVector对象 */
VectorList tv =new VectorList();
/*给Vector写入几String个对象*/
tv.add("One");
tv.add("Two");
tv.add("Three");
p.writeObject(tv); // 把tv写入流
p.flush();
objfile.close(); // 关闭文件对象

//pos.write("hello thank you for welcome!".getBytes());
pos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/******************************************************************************
* ****************************************************************************
* ********消费者, 把内容还原就可以用了 ********
* ****************************************************************************
*/

class Customer extends Thread {
private PipedInputStream pis;

public Customer(PipedInputStream pis) {
this.pis = pis;
}

public synchronized void run() {

try {
sleep(1000); //缓冲一下,很重要,管道流里没水的话,后果很严重
FileInputStream objfile = new FileInputStream("myvector.obj");
ObjectInputStream q = new ObjectInputStream(objfile);
/* 获取对象原始数据 */

VectorList myvector = (VectorList)q.readObject();
if(myvector.size()>0)myvector.print();

pis.close();

} catch (IOException e) {

e.printStackTrace();
} catch (ClassNotFoundException e) {

e.printStackTrace();
} catch (InterruptedException e) {

e.printStackTrace();
}
}

}

/*******************************************************************************
* *****************************************************************************
* ******** 构造一个序列化的Vector对象以保存数据库数据 ********
* *****************************************************************************
*/

class VectorList implements Serializable{

private Vector vect=new Vector();

public void add(Object obj){
this.vect.add(obj);
}
public void print(){
System.out.println(this.vect);
}

public Object get(int index) {
return this.vect.get(index);
}
public int size(){
return vect.size();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: