{"id":23863076,"url":"https://github.com/hungtcs/memory-cache","last_synced_at":"2025-02-22T12:23:43.505Z","repository":{"id":270451063,"uuid":"910419704","full_name":"hungtcs/memory-cache","owner":"hungtcs","description":"Simple Memory Cache For Go","archived":false,"fork":false,"pushed_at":"2024-12-31T08:52:58.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-31T09:24:29.528Z","etag":null,"topics":["cache","cachemanager","go","memory-cache"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/hungtcs/memory-cache","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/hungtcs.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":"2024-12-31T08:14:49.000Z","updated_at":"2024-12-31T08:48:52.000Z","dependencies_parsed_at":"2024-12-31T09:24:44.276Z","dependency_job_id":"4a7eb967-6cde-4e22-998b-939ba0e46e23","html_url":"https://github.com/hungtcs/memory-cache","commit_stats":null,"previous_names":["hungtcs/memory-cache"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hungtcs%2Fmemory-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hungtcs%2Fmemory-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hungtcs%2Fmemory-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hungtcs%2Fmemory-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hungtcs","download_url":"https://codeload.github.com/hungtcs/memory-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240171993,"owners_count":19759459,"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":["cache","cachemanager","go","memory-cache"],"created_at":"2025-01-03T07:38:58.509Z","updated_at":"2025-02-22T12:23:43.487Z","avatar_url":"https://github.com/hungtcs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Memory Cache\n\nThis is a simple memory cache manager.\n\nIt achieves thread safety through `sync.RWMutex` and automatically cleans up expired data using `time.Ticker`.\n\n## Features\n\n1. Thread-safe\n2. Support generics\n3. Expiration and auto cleanup\n4. Lightweight\n\n## API\n\n```go\n// public functions\nfunc NewMemoryCache[T any](optFuncs ...CacheOptionsFunc) *MemoryCache[T]\n\n// option functions\nfunc WithExpiration(expiration time.Duration) CacheOptionsFunc // global and peer item\nfunc WithMaxSize(maxSize int) CacheOptionsFunc // global only\nfunc WithLogger(log Logger) CacheOptionsFunc // global only\nfunc WithCleanupInterval(interval time.Duration) CacheOptionsFunc // global only\n\n// cache manager methods\nGet(key string) (_ *T, ok bool)\nTake(key string) (_ *T, ok bool)\nSet(key string, value T, optionFunc ...CacheOptionsFunc) (err error)\nUpdate(key string, updater func(value T) T, optionFunc ...CacheOptionsFunc) (ok bool)\nUpsert(key string, updater func(value *T) *T, optionFunc ...CacheOptionsFunc) (err error)\nHas(key string) (ok bool)\nDelete(key string)\nClear()\nSize() int\nIsEmpty() bool\nKeys() []string\nIterator() iter.Seq2[string, T] // for k, v := range cache.Iterator()\n```\n\n## Examples\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"log\"\n  \"os\"\n  \"time\"\n\n  memory_cache \"github.com/hungtcs/memory-cache\"\n)\n\nfunc main() {\n  cache := memory_cache.NewMemoryCache[string](\n    memory_cache.WithLogger(log.New(os.Stdout, \"[MEMORY_CACHE] \", log.Ldate|log.Ltime|log.Lmsgprefix)), // set logger\n    memory_cache.WithMaxSize(3),                   // may cause ErrCacheFull error\n    memory_cache.WithCleanupInterval(time.Second), // set cleanup interval\n    memory_cache.WithExpiration(time.Second*3),    // set default expiration\n  )\n  defer cache.Close()\n\n  cache.Set(\"1\", \"a\")\n  cache.Set(\"2\", \"b\")\n  cache.Set(\"3\", \"c\", memory_cache.WithExpiration(time.Second*10))\n  cache.Set(\"4\", \"d\") // error cache is full\n\n  time.Sleep(time.Second * 5) // 1 and 2 is expired\n\n  // only 3 is available\n  fmt.Println(\"cache size: \", cache.Size())\n  for key, val := range cache.Iterator() {\n    fmt.Printf(\"key=%q value=%q\\n\", key, val)\n  }\n}\n```\n\nYou can run this example on Go Playground \u003chttps://goplay.tools/snippet/RMsIhR99d6y\u003e.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhungtcs%2Fmemory-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhungtcs%2Fmemory-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhungtcs%2Fmemory-cache/lists"}