• [转载]数据库缓存算法思想与实现


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

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

  • 相关阅读:
    SaltStack
    python模块相关
    python optparse命令解析模块
    python ConfigParser模块 配置文件解析
    pssh 批量管理执行
    rsync+inotify 实现实时同步
    [译]17-spring基于java代码的配置元数据
    [译]16-spring基于注解的配置元数据
    [译]15-spring 自动装配
    [译]14-spring 集合元素的注入
  • 原文地址:https://www.cnblogs.com/sirkevin/p/2453748.html
Copyright © 2020-2023  润新知