github.com/GuanceCloud/cliutils@v1.1.21/diskcache/options.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package diskcache 7 8 import ( 9 "os" 10 "path/filepath" 11 "time" 12 ) 13 14 // A CacheOption used to set various options on DiskCache. 15 type CacheOption func(c *DiskCache) 16 17 // WithNoFallbackOnError disable fallback on fn() error. 18 // 19 // During Get(fn(data []btye)error{...}), if fn() failed with error, 20 // the next Get still get the same data from cache. 21 // If fallback disabled, the next read will read new data from cache, 22 // and the previous failed data skipped(and eventually dropped). 23 func WithNoFallbackOnError(on bool) CacheOption { 24 return func(c *DiskCache) { 25 c.noFallbackOnError = on 26 } 27 } 28 29 // WithNoLock set .lock on or off. 30 // 31 // File '.lock' used to exclude Open() on same path. 32 func WithNoLock(on bool) CacheOption { 33 return func(c *DiskCache) { 34 c.noLock = on 35 } 36 } 37 38 // WithNoPos set .pos on or off. 39 // 40 // The file '.pos' used to remember last Get() position, without '.pos', 41 // on process restart, some already-Get() data will Get() again in the 42 // new process, this maybe not the right action we expect. 43 func WithNoPos(on bool) CacheOption { 44 return func(c *DiskCache) { 45 c.noPos = on 46 } 47 } 48 49 // WithWakeup set duration on wakeup(default 3s), this wakeup time 50 // used to shift current-writing-file to ready-to-reading-file. 51 // 52 // NOTE: without wakeup, current-writing-file maybe not read-available 53 // for a long time. 54 func WithWakeup(wakeup time.Duration) CacheOption { 55 return func(c *DiskCache) { 56 if int64(wakeup) > 0 { 57 c.wakeup = wakeup 58 } 59 } 60 } 61 62 // WithBatchSize set file size, default 64MB. 63 func WithBatchSize(size int64) CacheOption { 64 return func(c *DiskCache) { 65 if size > 0 { 66 c.batchSize = size 67 } 68 } 69 } 70 71 // WithMaxDataSize set max single data size, default 32MB. 72 func WithMaxDataSize(size int32) CacheOption { 73 return func(c *DiskCache) { 74 if size > 0 { 75 c.maxDataSize = size 76 } 77 } 78 } 79 80 // WithCapacity set cache capacity, default unlimited. 81 func WithCapacity(size int64) CacheOption { 82 return func(c *DiskCache) { 83 if size > 0 { 84 c.capacity = size 85 } 86 } 87 } 88 89 // WithExtraCapacity add capacity to existing cache. 90 func WithExtraCapacity(size int64) CacheOption { 91 return func(c *DiskCache) { 92 if c.capacity+size > 0 { 93 c.capacity += size 94 if c.path != "" { 95 capVec.WithLabelValues(c.path).Set(float64(c.capacity)) 96 } 97 } 98 } 99 } 100 101 // WithNoSync enable/disable sync on cache write. 102 // 103 // NOTE: Without sync, the write performance 60~80 times faster for 512KB/1MB put, 104 // for smaller put will get more faster(1kb for 4000+ times). 105 func WithNoSync(on bool) CacheOption { 106 return func(c *DiskCache) { 107 c.noSync = on 108 } 109 } 110 111 // WithDirPermission set disk dir permission mode. 112 func WithDirPermission(perms os.FileMode) CacheOption { 113 return func(c *DiskCache) { 114 c.dirPerms = perms 115 } 116 } 117 118 // WithFilePermission set cache file permission mode. 119 func WithFilePermission(perms os.FileMode) CacheOption { 120 return func(c *DiskCache) { 121 c.filePerms = perms 122 } 123 } 124 125 // WithPath set disk dirname. 126 func WithPath(x string) CacheOption { 127 return func(c *DiskCache) { 128 c.path = filepath.Clean(x) 129 } 130 }