---
title: "简单http服务器缓存方案"
author: "monkeycn"
date: "2011-04-28T15:29:03Z"
tags: []
categories: ["编程语言"]
slug: "http"
draft: false
lastmod: "2011-04-28T15:29:03Z"
id: 1021699
url: "https://monkeycn.iteye.com/blog/1021699"
---

int i=0; // 读取索引数，从0开始
const int n = 5000; // 读取多少次后开始读缓存操作
void read_html()
{
     if ( i < n ) // 一般操作
    {
          read_from_disk();
          i++;
    }
    else if ( i==n ) // 建立缓存
    {
           read_from_disk();
           map.lock();
           map.add(html_file_name);
           map.unlock(); 
           i++;  
    }
   else // 读出缓存
   {
          map.readlock();
          pintf(map);
          map.readunlock();
   }
}

clean_cathe() // 清空缓存
{
    i =0;
    map.lock();
    map.remove(html_file_name);
    map.unlock();
}