[转载]数据库缓存算法思想与实现
2012-04-17 15:33
429 查看
这个算法我花一整整一天才做出来,真的好欣慰,现在写一个简单的示例程序,看的懂就看,看不懂回家再练练。
当然了,真正的应用程序肯定没有这么简单,不过我写出来了也没有人能看得懂,因为包含我的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();
}
}
转载:http://blog.csdn.net/aptweasel/article/details/1679506
当然了,真正的应用程序肯定没有这么简单,不过我写出来了也没有人能看得懂,因为包含我的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();
}
}
转载:http://blog.csdn.net/aptweasel/article/details/1679506
相关文章推荐
- 数据库缓存算法思想与实现
- Aprior算法简化算法——FP-Tree思想与实现(转载的)
- 数据库分表处理设计思想和实现
- C#实现的18位身份证格式验证算法[转载]
- 【转载】 数据库索引的实现原理
- PHP+TP框架实现获取微信JS-SDK使用权限签名算法需要的jsapi_ticket,并全局缓存
- [转载] 机器学习面试之算法思想简单梳理
- 初探浏览器缓存实现原理 --提高性能(转载)
- C#实现的18位身份证格式验证算法[转载]
- 数据库分表处理设计思想和实现
- memcached和redis两大数据库缓存系统实现对比
- O(n*lgn)时间复杂度的逆序对统计算法实现思想
- 二进制转十六进制 算法实现思想
- 基于关系数据库系统链式存储的树型结构数据,求某结点下的子树所有结点算法(t-sql语言实现)
- 用PHP去实现数据库查询结果缓存
- 白话经典算法系列之五 归并排序的实现(讲的真好)(转载)
- 转载:获取数据库中的所有表 (C#实现)
- 【算法和数据结构】分治思想之二分查找(C++实现)
- KMP算法基本思想与实现
- 用vc实现对超长数据库字段的操作(2)转载