{"id":42833628,"url":"https://github.com/feymanlee/cacheit","last_synced_at":"2026-01-30T11:36:35.448Z","repository":{"id":152380578,"uuid":"625853241","full_name":"feymanlee/cacheit","owner":"feymanlee","description":"A cache library for Golang that utilizes Go 1.18+ Generics and includes drivers for go-cache and go-redis[v8].","archived":false,"fork":false,"pushed_at":"2025-02-12T06:45:49.000Z","size":94,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-12T06:46:05.731Z","etag":null,"topics":["cache","go","go-cache","golang"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/feymanlee/cacheit","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/feymanlee.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":"2023-04-10T08:45:46.000Z","updated_at":"2025-02-12T06:40:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"b1c26b14-f129-467b-b131-d65fd15912fa","html_url":"https://github.com/feymanlee/cacheit","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/feymanlee/cacheit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feymanlee%2Fcacheit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feymanlee%2Fcacheit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feymanlee%2Fcacheit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feymanlee%2Fcacheit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/feymanlee","download_url":"https://codeload.github.com/feymanlee/cacheit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feymanlee%2Fcacheit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28911821,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-30T08:15:08.179Z","status":"ssl_error","status_checked_at":"2026-01-30T08:14:31.507Z","response_time":66,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","go","go-cache","golang"],"created_at":"2026-01-30T11:36:34.661Z","updated_at":"2026-01-30T11:36:35.437Z","avatar_url":"https://github.com/feymanlee.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cacheit\n\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/feymanlee/cacheit?style=flat-square)\n[![Go Report Card](https://goreportcard.com/badge/github.com/feymanlee/cacheit)](https://goreportcard.com/report/github.com/feymanlee/cacheit)\n[![Unit-Tests](https://github.com/feymanlee/logit/workflows/Unit-Tests/badge.svg)](https://github.com/feymanlee/cacheit/actions)\n[![Coverage Status](https://coveralls.io/repos/github/feymanlee/cacheit/badge.svg?branch=main)](https://coveralls.io/github/feymanlee/cacheit?branch=main)\n[![Go Reference](https://pkg.go.dev/badge/github.com/feymanlee/cacheit.svg)](https://pkg.go.dev/github.com/feymanlee/cacheit)\n[![License](https://img.shields.io/github/license/feymanlee/cacheit)](./LICENSE)\n\nGO 缓存库，支持 [go-redis](https://github.com/redis/go-redis/tree/v8)\n和 [go-cache](https://github.com/patrickmn/go-cache) 作为缓存驱动。它提供了一个简单的接口，允许您轻松地在您的项目中实现缓存。\n\n## 安装\n\n使用以下命令将 `cacheit` 添加到您的 Go 项目中：\n\n```sh\ngo get github.com/feymanlee/cacheit\n```\n\n## 接口定义\n\n```go\n// Driver cache driver interface\ntype Driver[V any] interface {\n    // Add Store an item in the cache if the key doesn't exist.\n    Add(key string, value V, t time.Duration) error\n    // Set Store an item in the cache for a given number of seconds.\n    Set(key string, value V, t time.Duration) error\n    // SetMany Store multiple items in the cache for a given number of seconds.\n    SetMany(many []Many[V]) error\n    // Forever Store an item in the cache indefinitely.\n    Forever(key string, value V) error\n    // Forget Remove an item from the cache.\n    Forget(key string) error\n    // Del Remove an item from the cache alia Forget.\n    Del(key string) error\n    // Flush Remove all items from the cache.\n    Flush() error\n    // Get Retrieve an item from the cache by key.\n    Get(key string) (V, error)\n    // Has Determined if an item exists in the cache.\n    Has(key string) (bool, error)\n    // Many Retrieve multiple items from the cache by key.\n    // Items not found in the cache will have a nil value.\n    Many(keys []string) (map[string]V, error)\n    // DelMany Remove multiple items from the cache.\n    DelMany(keys []string) error\n    // ForgetMany alias DelMany\n    // Remove multiple items from the cache.\n    ForgetMany(keys []string) error\n    // SetNumber set the int64 value of an item in the cache.\n    SetNumber(key string, value V, t time.Duration) error\n    // Increment the value of an item in the cache.\n    Increment(key string, n V) (V, error)\n    // Decrement the value of an item in the cache.\n    Decrement(key string, n V) (V, error)\n    // Remember Get an item from the cache, or execute the given Closure and store the result.\n    Remember(key string, ttl time.Duration, callback func() (V, error), force bool) (V, error)\n    // RememberForever Get an item from the cache, or execute the given Closure and store the result forever.\n    RememberForever(key string, callback func() (V, error), force bool) (V, error)\n    // RememberMany Get many item from the cache, or execute the given Closure and store the result.\n    RememberMany(keys []string, ttl time.Duration, callback func(notHitKeys []string) (map[string]V, error), force bool) (map[string]V, error)\n    // TTL Get cache ttl\n    TTL(key string) (time.Duration, error)\n    // WithCtx with context\n    WithCtx(ctx context.Context) Driver[V]\n    // WithSerializer with cache serializer\n    WithSerializer(serializer Serializer) Driver[V]\n}\n```\n\n## Usage\n\n### Base Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/feymanlee/cacheit\"\n\t\"github.com/go-redis/redis/v8\"\n\tgocache \"github.com/patrickmn/go-cache\"\n)\n\nfunc main() {\n\tredisClient := redis.NewClient(\u0026redis.Options{\n\t\tAddr: \"localhost:6379\",\n\t})\n\terr := cacheit.RegisterRedisDriver(\"redis_test\", redisClient, \"cache_prefix\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t// go-cache 客户端\n\tmemCache := gocache.New(5*time.Minute, 10*time.Minute)\n\terr = cacheit.RegisterGoCacheDriver(\"memory_test\", memCache, \"cache_prefix\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t// set default cache\n\tcacheit.SetDefault(\"redis_test\")\n\n\tdriver, err := cacheit.Use[string](\"memory_test\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t\n\terr = driver.Set(\"cache_key\", \"cache_value\", time.Minute)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t\n\tget, err := cacheit.UseDefault[string]().Get(\"cache_key\")\n\tif err != nil {\n\t\treturn\n\t}\n\tfmt.Println(get)\n}\n```\n\n### Add\nStore an item in the cache if the key doesn't exist.\n\n```go\nerr = driver.Add(\"key\", \"value\", time.Minute*10)\nif err != nil {\nlog.Println(\"Error adding cache:\", err)\n}\n```\n\n### Set\nStore an item in the cache for a given number of seconds.\n```go\nerr = driver.Set(\"key2\", \"value2\", time.Minute*5)\nif err != nil {\nlog.Println(\"Error setting cache:\", err)\n}\n```\n\n### Get\nRetrieve an item from the cache by key.\n\n```go\nvalue, err := driver.Get(\"key2\")\nif err != nil {\nlog.Println(\"Error getting cache:\", err)\n} else {\nlog.Println(\"Cache value:\", value)\n}\n```\n\n### SetMany\nStore multiple items in the cache for a given number of seconds.\n```go\nerr = driver.SetMany([]cacheit.Many[string]{\n{Key: \"key3\", Value: \"value3\", TTL: time.Minute * 10},\n{Key: \"key4\", Value: \"value4\", TTL: time.Minute * 15},\n})\nif err != nil {\nlog.Println(\"Error setting many caches:\", err)\n}\n```\n\n### Many\nRetrieve multiple items from the cache by key. Items not found in the cache will have a zero value.\n\n```go\nvalues, err := driver.Many([]string{\"key2\", \"key3\", \"key4\"})\nif err != nil {\nlog.Println(\"Error getting many caches:\", err)\n} else {\nlog.Println(\"Cache values:\", values)\n}\n```\n\n### Forever\nStore an item in the cache indefinitely.\n\n```go\nerr = driver.Forever(\"key5\", \"value5\")\nif err != nil {\nlog.Println(\"Error storing cache forever:\", err)\n}\n```\n\n### Forget\nRemove an item from the cache.\n\n```go\nerr = driver.Forget(\"key\")\nif err != nil {\nlog.Println(\"Error removing cache:\", err)\n}\n```\n\n### Flush\nRemove all items from the cache.\n\n```go\nerr = driver.Flush()\nif err != nil {\nlog.Println(\"Error flushing cache:\", err)\n}\n```\n\n### Has\nDetermined if an item exists in the cache.\n\n```go\nhas, err := driver.Has(\"key2\")\nif err != nil {\nlog.Println(\"Error checking if cache exists:\", err)\n} else if has {\nlog.Println(\"Cache key2 exists\")\n} else {\nlog.Println(\"Cache key2 does not exist\")\n}\n```\n\n### SetNumber\nSet the number value of an item in the cache.\n\n```go\nerr = driver.SetNumber(\"number_key\", 1, time.Minute*10)\nif err != nil {\nlog.Println(\"Error setting number cache:\", err)\n}\n```\n\n### Increment\nIncrement the value of an item in the cache.\n\n```go\nnewValue, err := driver.Increment(\"number_key\", 1)\nif err != nil {\nlog.Println(\"Error incrementing cache value:\", err)\n} else {\nlog.Println(\"Incremented cache value:\", newValue)\n}\n```\n\n### Increment\n\n```go\nnewValue, err := driver.Increment(\"number_key\", 1)\nif err != nil {\nlog.Println(\"Error incrementing cache value:\", err)\n} else {\nlog.Println(\"Incremented cache value:\", newValue)\n}\n```\n\n### Remember\n\n```go\nrememberValue, err := driver.Remember(\"remember_key\", time.Minute*10, func () (string, error) {\ntime.Sleep(time.Millisecond * 50)\nreturn \"remember_value\", nil\n})\nif err != nil {\nlog.Println(\"Error remembering cache:\", err)\n} else {\nlog.Println(\"Remember cache value:\", rememberValue)\n}\n```\n\n### RememberForever\n\n```go\nrememberForeverValue, err := driver.RememberForever(\"remember_forever_key\", func() (string, error) {\ntime.Sleep(time.Millisecond * 50)\nreturn \"remember_forever_value\", nil\n})\nif err != nil {\nlog.Println(\"Error remembering cache forever:\", err)\n} else {\nlog.Println(\"Remember cache value forever:\", rememberForeverValue)\n}\n```\n\n### TTL\n\n```go\nttl, err := driver.TTL(\"key2\")\nif err != nil {\nlog.Println(\"Error getting cache TTL:\", err)\n} else {\nlog.Println(\"Cache TTL:\", ttl)\n}\n```\n\n### WithCtx\n\n```go\nerr = driver.WithCtx(context.TODO()).Set(\"key2\", \"value2\", time.Minute*5)\nif err != nil {\nlog.Println(\"Error setting cache:\", err)\n}\n```\n\n## 更多功能\n\n请查阅源代码以了解更多功能和用法。\n\n## 贡献\n\n欢迎向项目贡献代码、提交 bug 报告或提出新功能建议。请务必遵循贡献指南。\n\n## 鸣谢\n\n\u003e [GoLand](https://www.jetbrains.com/go/?from=cacheit) A Go IDE with extended support for JavaScript, TypeScript, and databases。\n\n特别感谢 [JetBrains](https://www.jetbrains.com/?from=cacheit) 为开源项目提供免费的 [GoLand](https://www.jetbrains.com/go/?from=cacheit) 等 IDE 的授权  \n[\u003cimg src=\"https://account.jetbrains.com/static/images/jetbrains-logo-inv.svg\" width=\"150\"/\u003e](https://www.jetbrains.com/?from=cacheit)\n\n## 许可证\n\n本项目基于 MIT 许可证 发布。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeymanlee%2Fcacheit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffeymanlee%2Fcacheit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeymanlee%2Fcacheit/lists"}