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

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

当然了,真正的应用程序肯定没有这么简单,不过我写出来了也没有人能看得懂,因为包含我的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();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值