1
0

file.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cache
  15. import (
  16. "bytes"
  17. "crypto/md5"
  18. "encoding/gob"
  19. "encoding/hex"
  20. "encoding/json"
  21. "fmt"
  22. "io"
  23. "io/ioutil"
  24. "os"
  25. "path/filepath"
  26. "reflect"
  27. "strconv"
  28. "time"
  29. )
  30. // FileCacheItem is basic unit of file cache adapter.
  31. // it contains data and expire time.
  32. type FileCacheItem struct {
  33. Data interface{}
  34. Lastaccess time.Time
  35. Expired time.Time
  36. }
  37. // FileCache Config
  38. var (
  39. FileCachePath = "cache" // cache directory
  40. FileCacheFileSuffix = ".bin" // cache file suffix
  41. FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files.
  42. FileCacheEmbedExpiry time.Duration // cache expire time, default is no expire forever.
  43. )
  44. // FileCache is cache adapter for file storage.
  45. type FileCache struct {
  46. CachePath string
  47. FileSuffix string
  48. DirectoryLevel int
  49. EmbedExpiry int
  50. }
  51. // NewFileCache Create new file cache with no config.
  52. // the level and expiry need set in method StartAndGC as config string.
  53. func NewFileCache() Cache {
  54. // return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
  55. return &FileCache{}
  56. }
  57. // StartAndGC will start and begin gc for file cache.
  58. // the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
  59. func (fc *FileCache) StartAndGC(config string) error {
  60. var cfg map[string]string
  61. json.Unmarshal([]byte(config), &cfg)
  62. if _, ok := cfg["CachePath"]; !ok {
  63. cfg["CachePath"] = FileCachePath
  64. }
  65. if _, ok := cfg["FileSuffix"]; !ok {
  66. cfg["FileSuffix"] = FileCacheFileSuffix
  67. }
  68. if _, ok := cfg["DirectoryLevel"]; !ok {
  69. cfg["DirectoryLevel"] = strconv.Itoa(FileCacheDirectoryLevel)
  70. }
  71. if _, ok := cfg["EmbedExpiry"]; !ok {
  72. cfg["EmbedExpiry"] = strconv.FormatInt(int64(FileCacheEmbedExpiry.Seconds()), 10)
  73. }
  74. fc.CachePath = cfg["CachePath"]
  75. fc.FileSuffix = cfg["FileSuffix"]
  76. fc.DirectoryLevel, _ = strconv.Atoi(cfg["DirectoryLevel"])
  77. fc.EmbedExpiry, _ = strconv.Atoi(cfg["EmbedExpiry"])
  78. fc.Init()
  79. return nil
  80. }
  81. // Init will make new dir for file cache if not exist.
  82. func (fc *FileCache) Init() {
  83. if ok, _ := exists(fc.CachePath); !ok { // todo : error handle
  84. _ = os.MkdirAll(fc.CachePath, os.ModePerm) // todo : error handle
  85. }
  86. }
  87. // get cached file name. it's md5 encoded.
  88. func (fc *FileCache) getCacheFileName(key string) string {
  89. m := md5.New()
  90. io.WriteString(m, key)
  91. keyMd5 := hex.EncodeToString(m.Sum(nil))
  92. cachePath := fc.CachePath
  93. switch fc.DirectoryLevel {
  94. case 2:
  95. cachePath = filepath.Join(cachePath, keyMd5[0:2], keyMd5[2:4])
  96. case 1:
  97. cachePath = filepath.Join(cachePath, keyMd5[0:2])
  98. }
  99. if ok, _ := exists(cachePath); !ok { // todo : error handle
  100. _ = os.MkdirAll(cachePath, os.ModePerm) // todo : error handle
  101. }
  102. return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, fc.FileSuffix))
  103. }
  104. // Get value from file cache.
  105. // if non-exist or expired, return empty string.
  106. func (fc *FileCache) Get(key string) interface{} {
  107. fileData, err := FileGetContents(fc.getCacheFileName(key))
  108. if err != nil {
  109. return ""
  110. }
  111. var to FileCacheItem
  112. GobDecode(fileData, &to)
  113. if to.Expired.Before(time.Now()) {
  114. return ""
  115. }
  116. return to.Data
  117. }
  118. // GetMulti gets values from file cache.
  119. // if non-exist or expired, return empty string.
  120. func (fc *FileCache) GetMulti(keys []string) []interface{} {
  121. var rc []interface{}
  122. for _, key := range keys {
  123. rc = append(rc, fc.Get(key))
  124. }
  125. return rc
  126. }
  127. // Put value into file cache.
  128. // timeout means how long to keep this file, unit of ms.
  129. // if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
  130. func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error {
  131. gob.Register(val)
  132. item := FileCacheItem{Data: val}
  133. if timeout == FileCacheEmbedExpiry {
  134. item.Expired = time.Now().Add((86400 * 365 * 10) * time.Second) // ten years
  135. } else {
  136. item.Expired = time.Now().Add(timeout)
  137. }
  138. item.Lastaccess = time.Now()
  139. data, err := GobEncode(item)
  140. if err != nil {
  141. return err
  142. }
  143. return FilePutContents(fc.getCacheFileName(key), data)
  144. }
  145. // Delete file cache value.
  146. func (fc *FileCache) Delete(key string) error {
  147. filename := fc.getCacheFileName(key)
  148. if ok, _ := exists(filename); ok {
  149. return os.Remove(filename)
  150. }
  151. return nil
  152. }
  153. // Incr will increase cached int value.
  154. // fc value is saving forever unless Delete.
  155. func (fc *FileCache) Incr(key string) error {
  156. data := fc.Get(key)
  157. var incr int
  158. if reflect.TypeOf(data).Name() != "int" {
  159. incr = 0
  160. } else {
  161. incr = data.(int) + 1
  162. }
  163. fc.Put(key, incr, FileCacheEmbedExpiry)
  164. return nil
  165. }
  166. // Decr will decrease cached int value.
  167. func (fc *FileCache) Decr(key string) error {
  168. data := fc.Get(key)
  169. var decr int
  170. if reflect.TypeOf(data).Name() != "int" || data.(int)-1 <= 0 {
  171. decr = 0
  172. } else {
  173. decr = data.(int) - 1
  174. }
  175. fc.Put(key, decr, FileCacheEmbedExpiry)
  176. return nil
  177. }
  178. // IsExist check value is exist.
  179. func (fc *FileCache) IsExist(key string) bool {
  180. ret, _ := exists(fc.getCacheFileName(key))
  181. return ret
  182. }
  183. // ClearAll will clean cached files.
  184. // not implemented.
  185. func (fc *FileCache) ClearAll() error {
  186. return nil
  187. }
  188. // check file exist.
  189. func exists(path string) (bool, error) {
  190. _, err := os.Stat(path)
  191. if err == nil {
  192. return true, nil
  193. }
  194. if os.IsNotExist(err) {
  195. return false, nil
  196. }
  197. return false, err
  198. }
  199. // FileGetContents Get bytes to file.
  200. // if non-exist, create this file.
  201. func FileGetContents(filename string) (data []byte, e error) {
  202. return ioutil.ReadFile(filename)
  203. }
  204. // FilePutContents Put bytes to file.
  205. // if non-exist, create this file.
  206. func FilePutContents(filename string, content []byte) error {
  207. return ioutil.WriteFile(filename, content, os.ModePerm)
  208. }
  209. // GobEncode Gob encodes file cache item.
  210. func GobEncode(data interface{}) ([]byte, error) {
  211. buf := bytes.NewBuffer(nil)
  212. enc := gob.NewEncoder(buf)
  213. err := enc.Encode(data)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return buf.Bytes(), err
  218. }
  219. // GobDecode Gob decodes file cache item.
  220. func GobDecode(data []byte, to *FileCacheItem) error {
  221. buf := bytes.NewBuffer(data)
  222. dec := gob.NewDecoder(buf)
  223. return dec.Decode(&to)
  224. }
  225. func init() {
  226. Register("file", NewFileCache)
  227. }