{"id":18898256,"url":"https://github.com/powerpuffpenguin/gcache","last_synced_at":"2025-07-07T01:32:53.445Z","repository":{"id":124271797,"uuid":"360779394","full_name":"powerpuffpenguin/gcache","owner":"powerpuffpenguin","description":"golang cache interface and some algorithm implementation","archived":false,"fork":false,"pushed_at":"2021-04-28T03:25:51.000Z","size":52,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-31T08:45:30.730Z","etag":null,"topics":["2q-cache","cache","fifo","fifo-cache","golang","lfu","lfu-cache","lru","lru-cache","lru-k","lru-k-cache","lruk","lruk-cache"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/powerpuffpenguin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-23T06:02:56.000Z","updated_at":"2022-05-12T16:59:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"56aaf6c3-f084-406b-9390-b3ef5853462b","html_url":"https://github.com/powerpuffpenguin/gcache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/powerpuffpenguin%2Fgcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/powerpuffpenguin%2Fgcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/powerpuffpenguin%2Fgcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/powerpuffpenguin%2Fgcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/powerpuffpenguin","download_url":"https://codeload.github.com/powerpuffpenguin/gcache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239879317,"owners_count":19712176,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2q-cache","cache","fifo","fifo-cache","golang","lfu","lfu-cache","lru","lru-cache","lru-k","lru-k-cache","lruk","lruk-cache"],"created_at":"2024-11-08T08:41:41.918Z","updated_at":"2025-02-20T17:14:13.651Z","avatar_url":"https://github.com/powerpuffpenguin.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gcache\n\ngolang cache interface and some algorithm implementation\n\n* fifo\n* lfu\n* lru\n* lru-k\n* 2q\n\n# example \n\n```\ngithub.com/powerpuffpenguin/gcache\n```\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/powerpuffpenguin/gcache\"\n)\n\nfunc main() {\n\tc := gcache.NewLRU(\n\t\tgcache.WithLRUCapacity(3),\n\t)\n\tfor i := 0; i \u003c 4; i++ {\n\t\tkey := i\n\t\tval := fmt.Sprintf(`val %d`, i)\n\t\tc.Put(key, val)\n\t}\n\n\tfor i := 0; i \u003c 4; i++ {\n\t\tkey := i\n\t\tval, exists := c.Get(key)\n\t\tif exists {\n\t\t\tfmt.Printf(\"key=%v val=%v\\n\", key, val)\n\t\t} else {\n\t\t\tfmt.Printf(\"key=%v not exists\\n\", key)\n\t\t}\n\t}\n}\n```\n\n# interface \n\ngcache provides two interface for users to use.\n\n1. **Cache** goroutine safe cache\n2. **LowCache** Implementation of goroutine not safe cache low-level algorithm\n\n## Cache\n\nUsually you should use the Cache interface directly.\n\n```\ntype Cache interface {\n\t// Add the value to the cache, only when the key does not exist\n\tAdd(key, value interface{}) (added bool)\n\t// Put key value to cache\n\tPut(key, value interface{})\n\t// Get return cache value\n\tGet(key interface{}) (value interface{}, exists bool)\n\t// BatchPut pairs to cache\n\tBatchPut(pair ...interface{})\n\t// BatchGet return cache values\n\tBatchGet(key ...interface{}) (vals []Value)\n\t// Delete key from cache\n\tDelete(key ...interface{}) (changed int)\n\t// Len returns the number of cached data\n\tLen() (count int)\n\t// Clear all cached data\n\tClear()\n}\n```\n\ngcache provides several NewXXX functions for creating Cache. In addition, several WithXXX settings are provided to guide parameters for the caching algorithm.\n\n```\ncapacity := 1000\nexpiry := time.Minute\nduration := time.Minute\n// basic\nlru := gcache.NewLRU(\n\tgcache.WithLRUCapacity(capacity), // cache capacity\n\tgcache.WithLRUExpiry(expiry),     // inactivity expiration time\n\tgcache.WithLRUClear(duration),    // the timer clears the expired cache\n)\nlfu := gcache.NewLFU(\n\tgcache.WithLFUCapacity(capacity),\n\tgcache.WithLFUExpiry(expiry),\n\tgcache.WithLFUClear(duration),\n)\nfifo := gcache.NewFIFO(\n// WithFIFOXXX\n)\n// complex\nlruk2 := gcache.NewLRUK(\n\tgcache.WithLRUK(2),\n\tgcache.WithLRUKHistoryOnlyKey(true),        // if true history only save key not save value, false save key and value\n\tgcache.WithLRUKHistory(gcache.NewLowLRU()), // history use lru\n)\nlruk3 := gcache.NewLRUK(\n\tgcache.WithLRUK(3),\n)\n// 2q\nc2q := gcache.NewLRUK(\n\tgcache.WithLRUK(2),\n\tgcache.WithLRUKHistoryOnlyKey(false),\n\tgcache.WithLRUKHistory(gcache.NewLowFIFO()), // history use fifo\n)\n```\n\n## LowCache\n\nThe LowCache interface is a low-level implementation that implements the basic algorithm.\n\n```\n// Low-level caching is usually only used when combining multiple caching algorithms\ntype LowCache interface {\n\t// Clear Expired cache\n\tClearExpired()\n\t// Add the value to the cache, only when the key does not exist\n\tAdd(key, value interface{}) (added bool)\n\t// Put key value to cache\n\tPut(key, value interface{}) (delkey, delval interface{}, deleted bool)\n\t// Get return cache value\n\tGet(key interface{}) (value interface{}, exists bool)\n\t// Delete key from cache\n\tDelete(key ...interface{}) (changed int)\n\t// Len returns the number of cached data\n\tLen() int\n\t// Clear all cached data\n\tClear()\n}\n```\n\ngcache provides several NewLowXXX functions for creating LowCache. In addition, several WithLowXXX settings are provided to guide parameters for the caching algorithm.\n\n# fifo\n\n```\ngcache.NewFIFO(\n\tgcache.WithFIFOCapacity(1000),\n\tgcache.WithFIFOExpiry(time.Minute),\n\tgcache.WithFIFOClear(time.Minute*10),\n)\n```\n# lfu\n```\ngcache.NewLFU(\n\tgcache.WithLFUCapacity(1000),\n\tgcache.WithLFUExpiry(time.Minute),\n\tgcache.WithLFUClear(time.Minute*10),\n)\n```\n\n# lru\n\n```\ngcache.NewLRU(\n\tgcache.WithLRUCapacity(1000),\n\tgcache.WithLRUExpiry(time.Minute), \n\tgcache.WithLRUClear(time.Minute*10),\n)\n```\n\n# lru-k\n\n```\ngcache.NewLRUK(\n\tgcache.WithLRUKCapacity(1000),\n\tgcache.WithLRUKExpiry(time.Minute),\n\tgcache.WithLRUKClear(time.Minute*10),\n\n\tgcache.WithLRUK(2),\n\tgcache.WithLRUKHistoryOnlyKey(false),\n\tgcache.WithLRUKHistory(gcache.NewLowLRU()),\n)\n```\n\n# 2q\n\n```\ngcache.NewLRUK(\n\tgcache.WithLRUKCapacity(1000),\n\tgcache.WithLRUKExpiry(time.Minute),\n\tgcache.WithLRUKClear(time.Minute*10),\n\n\tgcache.WithLRUK(2),\n\tgcache.WithLRUKHistoryOnlyKey(false),\n\tgcache.WithLRUKHistory(gcache.NewLowFIFO(\n\t\tgcache.WithLowFIFOCapacity(1000),\n\t\tgcache.WithLowFIFOExpiry(time.Minute),\n\t)),\n)\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpowerpuffpenguin%2Fgcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpowerpuffpenguin%2Fgcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpowerpuffpenguin%2Fgcache/lists"}