cachefs

package module
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

README

cachefs

English

介绍

通过go的http.FileServer(http.Dir(path))可以很方便的创建文件服务器 go的http.Dir如果没有错误每次Open调用都会进行一次os.Open调用,os.Stat调用,至少一次的(*os.File).Read调用,这些最终会调用syscall包的函数进行系统调用,即使文件没有修改,这可以优化。 本包提供 HttpCacheFs ,可以将 http.Dir(path) 替换为 cachefs.HttpCacheFs(path) ,在被读取文件没有修改(当前通过比较修改时间判断),可以减少系统调用(具体是避免os.Open,(*os.File).Read),提高性能。

实现原理
HttpCacheFs

实现了http.FileSystem 接口 内部有一个哈希表 key是path value的类型是*CacheFs,用作缓存 如果path已经被哈希表缓存,直接返回缓存,从而避免os.Open

CacheFs

实现了http.File接口 内部用Buf保存文件数据,保留文件的(*os.File)句柄 当Read或Seek方法被调用时,先通过比较修改时间判断文件有没有修改

  • 如果没有修改,调用Buf的Read或Seek方法,避免(*os.File).Read或(*os.File).Seek
  • 如果有修改,重新读取文件数据,并更新HttpCacheFs的缓存,再调用Read或Seek方法

Close方法为了配合http.FileServer,永远返回nil,并且不关闭文件句柄

当Readdir或Stat方法被调用时,先通过比较修改时间判断文件有没有修改

  • 如果没有修改,调用os.File的Readdir或Stat
  • 如果有修改,重新读取文件数据,并更新HttpCacheFs的缓存,再调用Readdir或Stat方法
Buf

实现了io.Reader接口 实现了io.Seeker接口 Buf将[]byte封装成数据流,读取到末尾返回 io.EOF 后下次读取会从头开始

参与贡献
  1. 创建一个issue
  2. Fork 本仓库
  3. 新建 Fork_xxx 分支
  4. 提交代码
  5. 新建 Pull Request

Documentation

Overview

Package cachefs 实现了缓存文件系统

缓存文件系统用于在 http.FileSystem 默认实现需要优化 通过比较修改时间,缓存文件系统在Open和Read时如果没有修改,减少系统调用(具体是避免os.Open,(*os.File).Read)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buf

type Buf struct {
	// contains filtered or unexported fields
}

Buf 循环缓存

func NewBuf

func NewBuf(buf []byte) Buf

NewBuf 创建循环缓存

func (*Buf) Copy

func (buf *Buf) Copy() Buf

Copy 浅拷贝一个循环缓存

func (*Buf) Empty

func (buf *Buf) Empty() bool

Empty 判断缓存是否到末尾

func (*Buf) Read

func (buf *Buf) Read(p []byte) (n int, err error)

Read 实现 io.Reader 接口,读取缓存内容 读取到末尾返回 io.EOF 后下次读取会从头开始

func (*Buf) Seek

func (buf *Buf) Seek(offset int64, whence int) (int64, error)

Seek 实现 io.Seeker 接口

type CacheFs

type CacheFs struct {
	// contains filtered or unexported fields
}

CacheFs 缓存文件系统

通过比较修改时间,在Open和Read时如果没有修改,减少系统调用(具体是避免os.Open,(*os.File).Read)

func NewCacheFs

func NewCacheFs(name string) (fs *CacheFs, err error)

NewCacheFs 创建缓存文件系统

func (*CacheFs) Close

func (fs *CacheFs) Close() error

Close 关闭

为了能配合 http.FileServer ,永远返回nil,并且不关闭文件句柄

func (*CacheFs) Copy

func (fs *CacheFs) Copy() *CacheFs

Copy 对自身进行浅拷贝

func (*CacheFs) Read

func (fs *CacheFs) Read(p []byte) (n int, err error)

Read 实现 io.Reader 接口,如果没有修改,将返回缓存内容

func (*CacheFs) Readdir

func (fs *CacheFs) Readdir(count int) ([]fs.FileInfo, error)

Readdir 返回目录信息

func (*CacheFs) Seek

func (fs *CacheFs) Seek(offset int64, whence int) (int64, error)

Seek 实现 io.Seeker 接口,如果没有修改,将移动缓存内容偏移量

func (*CacheFs) Stat

func (fs *CacheFs) Stat() (fs.FileInfo, error)

Stat 返回文件信息

如果没有修改,避免 os.Open 系统调用

type HttpCacheFs

type HttpCacheFs struct {
	// contains filtered or unexported fields
}

HttpCacheFs Http缓存文件系统

Example
path := "path"
http.Handle("/", http.FileServer(NewHttpCacheFs(path)))
Output:

func NewHttpCacheFs

func NewHttpCacheFs(path string) *HttpCacheFs

NewHttpCacheFs 创建NewHttpCacheFs

  • path 是相对的路径 相当于 http.Dir

func (*HttpCacheFs) Open

func (fs *HttpCacheFs) Open(name string) (http.File, error)

Open 实现 http.FileSystem 接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL