GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cache-mem/options.go at main · ubgo/cache-mem Expand file tree/ Copy pathoptions.goMore file actions 121 lines (103 loc) · 4.79 KB / Copy pathoptions.goFile metadata and controls121 lines (103 loc) · 4.79 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 // options.go — functional options + resolved config for the cache (package memcache, github.com/ubgo/cache-mem). // // Package role: memcache is the in-memory adapter of the ubgo/cache // family. See doc.go for the package overview. // // This file: the config struct (resolved option set), the Option type, // every WithX constructor option (WithShards/WithMaxEntries/WithMaxBytes/ // WithWeigher/WithOnEvict/WithSweepInterval/WithClock/WithCheckpoint/ // WithAOF/WithPolicy), defaults(), and nextPow2. Zero-valued caps and // intervals mean "feature disabled" (unbounded / lazy-only / no // persistence). nextPow2 enforces the power-of-two shard-count invariant // that shardFor depends on. // // AI-context: pure config plumbing — options mutate a config, New // consumes it once. Note defaults() sets policy to AdaptiveWTinyLFU // EXPLICITLY (not via the LRU iota zero value) — W-TinyLFU is the // intended default; relying on the zero value would silently flip it. package memcache import ( "time" "github.com/ubgo/cache" ) // Weigher returns the cost of a value for size-aware (max-bytes) eviction. // Default counts raw value bytes. type Weigher func(val []byte) int64 // config is the resolved option set. Built by defaults() then mutated by the // applied Option funcs in New. Zero-valued caps/intervals mean the // corresponding feature is disabled (unbounded / lazy-only / no persistence). type config struct { shards int maxEntries int64 // global; 0 = unlimited maxBytes int64 // global; 0 = unlimited weigher Weigher onEvict func(key string, cause cache.EvictionCause) clock func() time.Time sweepInterval time.Duration // 0 = lazy expiry only policy Policy ckptPath string ckptInterval time.Duration aofPath string } // Option configures New. type Option func(*config) // WithShards sets the shard count (default 16). Rounded up to a power of two. func WithShards(n int) Option { return func(c *config) { c.shards = n } } // WithMaxEntries caps total entries; least-recently-used are evicted past it. func WithMaxEntries(n int64) Option { return func(c *config) { c.maxEntries = n } } // WithMaxBytes caps total weighed size; LRU eviction enforces it. func WithMaxBytes(n int64) Option { return func(c *config) { c.maxBytes = n } } // WithWeigher overrides the per-entry cost function used by WithMaxBytes. func WithWeigher(w Weigher) Option { return func(c *config) { c.weigher = w } } // WithOnEvict registers a callback fired for every eviction with its cause. func WithOnEvict(fn func(key string, cause cache.EvictionCause)) Option { return func(c *config) { c.onEvict = fn } } // WithSweepInterval runs a background goroutine that proactively drops expired // entries every d (in addition to lazy expiry on read). Stopped by Close. func WithSweepInterval(d time.Duration) Option { return func(c *config) { c.sweepInterval = d } } // WithClock overrides the time source (tests only). func WithClock(fn func() time.Time) Option { return func(c *config) { c.clock = fn } } // WithCheckpoint periodically snapshots the cache to path every interval // (atomic write via temp file + rename) and writes a final checkpoint on // Close. Pair with RestoreFromFile at startup for crash-resilient warm state. func WithCheckpoint(path string, interval time.Duration) Option { return func(c *config) { c.ckptPath = path; c.ckptInterval = interval } } // WithAOF enables append-only-file durability at path: every write // (Set/SetMulti/SetNX/Expire/Incr/Decr/Del/Flush) is fsync-appended to the // log, and an existing log is replayed into memory on New before appends // resume. Near-zero-loss durability at the cost of a sync per write; call // CompactAOF periodically to bound the log. Composes with WithCheckpoint. func WithAOF(path string) Option { return func(c *config) { c.aofPath = path } } // WithPolicy selects the eviction algorithm (default LRU). AdaptiveWTinyLFU // trades a little memory for substantially higher hit rates on skewed and // scan-heavy workloads. func WithPolicy(p Policy) Option { return func(c *config) { c.policy = p } } // defaults returns the baseline config. Note policy is explicitly set to // AdaptiveWTinyLFU here even though LRU is the iota zero value: the default // policy is W-TinyLFU (PLAN Q6 / §1.5), and relying on the zero value would // silently make LRU the default instead. func defaults() config { return config{ shards: 16, weigher: func(v []byte) int64 { return int64(len(v)) }, clock: time.Now, policy: AdaptiveWTinyLFU, // PLAN Q6 / §1.5: W-TinyLFU is the default } } // nextPow2 rounds n up to the next power of two (minimum 1). The shard count // must be a power of two so shardFor's modulo collapses to an AND with the // mask. func nextPow2(n int) int { if n < 1 { return 1 } p := 1 for p < n { p <<= 1 } return p } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |