{"id":13819518,"url":"https://github.com/Pacific73/gorm-cache","last_synced_at":"2025-05-16T04:34:01.430Z","repository":{"id":36979473,"uuid":"481964706","full_name":"Pacific73/gorm-cache","owner":"Pacific73","description":"gorm v2的即插即用、无需修改代码的旁路缓存。An easy-to-use look-aside cache solution for gorm v2 users. ","archived":false,"fork":false,"pushed_at":"2024-06-14T07:20:14.000Z","size":57,"stargazers_count":113,"open_issues_count":11,"forks_count":29,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-19T18:48:46.810Z","etag":null,"topics":["cache","golang","gorm"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pacific73.png","metadata":{"files":{"readme":"README.ZH_CN.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":"2022-04-15T13:19:16.000Z","updated_at":"2024-11-04T03:10:39.000Z","dependencies_parsed_at":"2022-08-18T01:05:30.364Z","dependency_job_id":"ad746148-4982-4465-810f-4307e1f09b97","html_url":"https://github.com/Pacific73/gorm-cache","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pacific73%2Fgorm-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pacific73%2Fgorm-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pacific73%2Fgorm-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pacific73%2Fgorm-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pacific73","download_url":"https://codeload.github.com/Pacific73/gorm-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254470304,"owners_count":22076566,"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","golang","gorm"],"created_at":"2024-08-04T08:00:49.672Z","updated_at":"2025-05-16T04:34:01.007Z","avatar_url":"https://github.com/Pacific73.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"[![GoVersion](https://img.shields.io/github/go-mod/go-version/Pacific73/gorm-cache)](https://github.com/Pacific73/gorm-cache/blob/master/go.mod)\n[![Release](https://img.shields.io/github/v/release/Pacific73/gorm-cache)](https://github.com/Pacific73/gorm-cache/releases)\n[![Apache-2.0 license](https://img.shields.io/badge/license-Apache2.0-brightgreen.svg)](https://opensource.org/licenses/Apache-2.0)\n\n[English Version](./README.md) | [中文版本](./README.ZH_CN.md)\n\n`gorm-cache` 旨在为gorm v2用户提供一个即插即用的旁路缓存解决方案。本缓存只适用于数据库表单主键时的场景。\n\n我们提供2种存储介质：\n\n1. 内存 (所有数据存储在单服务器的内存中)\n2. Redis (所有数据存储在redis中，如果你有多个实例使用本缓存，那么他们不共享redis存储空间)\n\n## 使用说明\n\n```go\nimport (\n    \"context\"\n    \"github.com/Pacific73/gorm-cache/cache\"\n    \"github.com/go-redis/redis\"\n)\n\nfunc main() {\n    dsn := \"user:pass@tcp(127.0.0.1:3306)/database_name?charset=utf8mb4\"\n    db, _ := gorm.Open(mysql.Open(dsn), \u0026gorm.Config{})\n    \n    redisClient := redis.NewClient(\u0026redis.Options{\n        Addr: \"localhost:6379\",    \n    })\n    \n    cache, _ := cache.NewGorm2Cache(\u0026config.CacheConfig{\n        CacheLevel:           config.CacheLevelAll,\n        CacheStorage:         config.CacheStorageRedis,\n        RedisConfig:          cache.NewRedisConfigWithClient(redisClient),\n        InvalidateWhenUpdate: true, // when you create/update/delete objects, invalidate cache\n        CacheTTL:             5000, // 5000 ms\n        CacheMaxItemCnt:      5,    // if length of objects retrieved one single time \n                                    // exceeds this number, then don't cache\n    })\n    // More options in `config.config.go`\n    db.Use(cache)    // use gorm plugin\n    // cache.AttachToDB(db)\n\n    var users []User\n    \n    db.Where(\"value \u003e ?\", 123).Find(\u0026users) // search cache not hit, objects cached\n    db.Where(\"value \u003e ?\", 123).Find(\u0026users) // search cache hit\n    \n    db.Where(\"id IN (?)\", []int{1, 2, 3}).Find(\u0026users) // primary key cache not hit, users cached\n    db.Where(\"id IN (?)\", []int{1, 3}).Find(\u0026users) // primary key cache hit\n}\n```\n\n在gorm中主要有5种操作（括号中是gorm中对应函数名）:\n\n1. Query (First/Take/Last/Find/FindInBatches/FirstOrInit/FirstOrCreate/Count/Pluck)\n2. Create (Create/CreateInBatches/Save)\n3. Delete (Delete)\n4. Update (Update/Updates/UpdateColumn/UpdateColumns/Save)\n5. Row (Row/Rows/Scan)\n\n我们不支持Row操作的缓存。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPacific73%2Fgorm-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPacific73%2Fgorm-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPacific73%2Fgorm-cache/lists"}