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.
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
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
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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。