1 Star 0 Fork 0

秋来冬风 / cachefs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.en.md 2.41 KB
一键复制 编辑 原始数据 按行查看 历史
秋来冬风 提交于 2023-03-01 14:07 . 秋来冬风:Readdir实现

cachefs

English

Introduction

Through http. FileServer (http. Dir (path)) of go, you can easily create a file server If there is no error in http.Dir of go, each Open call will make one os.Open, os.Stat, and at least one (* os.File). Read call. These will eventually call the syscall package function for system calls. Even if the file is not modified, this can be optimized. This package provides HttpCacheFs, which can replace http.Dir (path) with cachefs.HttpCacheFs (path). When the read file is not modified (currently judged by comparing the modification time), it can reduce system calls (specifically avoid os.Open, (* os. File). Read) and improve performance.

Implementation principle

HttpCacheFs

Implemented http. FileSystem Interface

There is an internal hash table whose key is path value and whose type is *CacheFs, which is used as cache

If the path has been cached by the hash table, the cache is returned directly to avoid os.Open

CacheFs

Implemented http. File Interface

Use Buf internally to save file data and keep the (*os.File) handle of the file

When the Read or Seek method is called, first determine whether the file has been modified by comparing the modification time

  • If not modified, call Buf's Read or Seek method to avoid (*os.File). Read or (*os.File). Seek

  • If there is any modification, re-read the file data, update the cache of HttpCacheFs, and then call the Read or Seek method

The Close method is used to match http. FileServer, always return nil, and do not close the file handle

When the Readdir or Stat method is called, first determine whether the file has been modified by comparing the modification time

  • If it is not modified, call Readdir or Stat of os.File

  • If there is any modification, re-read the file data, update the cache of HttpCacheFs, and then call the Readdir or Stat method

Buf

Implemented io. Reader Interface

Implemented io. Seeker Interface

Buf encapsulates []byte into a data stream, reads it to the end and returns io. EOF The next reading will start from the beginning

Participation contribution

  1. Create an issue
  2. Fork warehouse
  3. Create a new Fork_ Xxx branch
  4. Submit code
  5. Create a new Pull Request
Go
1
https://gitee.com/qiulaidongfeng/cachefs.git
git@gitee.com:qiulaidongfeng/cachefs.git
qiulaidongfeng
cachefs
cachefs
v0.7.0

搜索帮助