{"id":13411780,"url":"https://github.com/ulovecode/gdcache","last_synced_at":"2026-01-14T14:16:12.902Z","repository":{"id":47843085,"uuid":"387791068","full_name":"ulovecode/gdcache","owner":"ulovecode","description":"gdcache is a pure non-intrusive cache library implemented by golang, you can use it to implement your own cache.","archived":false,"fork":false,"pushed_at":"2021-10-14T17:31:29.000Z","size":315,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-07-31T20:48:59.274Z","etag":null,"topics":["cache","go","mysql","orm","sql"],"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/ulovecode.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}},"created_at":"2021-07-20T12:52:02.000Z","updated_at":"2024-01-18T05:02:20.000Z","dependencies_parsed_at":"2022-09-02T16:35:45.221Z","dependency_job_id":null,"html_url":"https://github.com/ulovecode/gdcache","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"purl":"pkg:github/ulovecode/gdcache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulovecode%2Fgdcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulovecode%2Fgdcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulovecode%2Fgdcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulovecode%2Fgdcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ulovecode","download_url":"https://codeload.github.com/ulovecode/gdcache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ulovecode%2Fgdcache/sbom","scorecard":{"id":908061,"data":{"date":"2025-08-11","repo":{"name":"github.com/ulovecode/gdcache","commit":"efffe2595c0b9526ab24841e9291c82ea99c01cc"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.4,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Code-Review","score":0,"reason":"Found 0/29 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/ci.yml:1","Warn: no topLevel permission defined: .github/workflows/go.yml:1","Warn: no topLevel permission defined: .github/workflows/release.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:9: update your workflow using https://app.stepsecurity.io/secureworkflow/ulovecode/gdcache/ci.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/ulovecode/gdcache/ci.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/go.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/ulovecode/gdcache/go.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/go.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/ulovecode/gdcache/go.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/ulovecode/gdcache/release.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:36: update your workflow using https://app.stepsecurity.io/secureworkflow/ulovecode/gdcache/release.yml/main?enable=pin","Warn: downloadThenRun not pinned by hash: .github/workflows/ci.yml:19","Info:   0 out of   6 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   1 downloadThenRun dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 2 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-24T18:06:17.663Z","repository_id":47843085,"created_at":"2025-08-24T18:06:17.663Z","updated_at":"2025-08-24T18:06:17.663Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28422463,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T13:30:50.153Z","status":"ssl_error","status_checked_at":"2026-01-14T13:29:08.907Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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","mysql","orm","sql"],"created_at":"2024-07-30T20:01:16.860Z","updated_at":"2026-01-14T14:16:12.877Z","avatar_url":"https://github.com/ulovecode.png","language":"Go","readme":"\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [gdcache](#gdcache)\n    - [Features](#features)\n    - [Core principle](#core-principle)\n    - [Save memory](#save-memory)\n    - [Installation](#installation)\n    - [Quick Start](#quick-start)\n        - [Gorm usage](#gorm-usage)\n        - [Xorm usage](#xorm-usage)\n        - [Native SQL usage](#native-sql-usage)\n    - [How to use](#how-to-use)\n    - [Contributing](#contributing)\n    - [License](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n# gdcache\n\ngdcache is a pure non-intrusive cache library implemented by golang, you can use it to implement your own\ncache. [中文文档](https://github.com/ulovecode/gdcache/blob/main/README_CN.md)\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/ulovecode/gdcache)](https://goreportcard.com/report/github.com/ulovecode/gdcache)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/ulovecode/gdcache)](https://pkg.go.dev/github.com/ulovecode/gdcache)\n[![codecov](https://codecov.io/gh/ulovecode/gdcache/branch/main/graph/badge.svg?token=4GNQINA6QX)](https://codecov.io/gh/ulovecode/gdcache)\n\n## Features\n\n- Automatically cache sql\n- Reuse id cache\n- Adapt to Xorm and Gorm framework\n- Support cache joint key\n- Lightweight\n- Non-invasive\n- High performance\n- Flexible\n\n## Core principle\n\nThe core principle of gdcache is to convert sql into id and cache it, and cache the entity corresponding to id. In this\nway, each sql has the same id and can reuse the corresponding entity content.\n\n![img.png](doc/flow-img.png)\n\nAs shown in the figure above, each piece of sql can be converted to the corresponding sql, and the bottom layer will\nreuse these ids. If these ids are not queried, because we don’t know whether they are out of date or because these\nvalues do not exist in the database, we will all be in the database and access these entities that cannot be retrieved\nfrom the cache from the database. Get it once, and if it can get it, it will cache it once.\n\n## Save memory\n\nThe conventional caching framework will cache the content of the result, but the gdcache cache library is different from\nit. It will only cache the id of the result and find the value through the id. The advantage of this is that the value\ncan be reused, and the value corresponding to id will only be cached once.\n\n## Installation\n\n```shell\ngo get github.com/ulovecode/gdcache\n```\n\n## Quick Start\n\n- The class to be cached must implement the `TableName()` method, and use `cache:\"id\"` to indicate the cached key. The\n  default is to cache by `id`, and the value of the `cache` tag corresponds to Fields in the database, usually can be\n  ignored.\n\n```go\ntype User struct {\n    Id   uint64 `cache:\"id\"` // Or omit the tag\n    Name string\n    Age  int\n}\n\nfunc (u User) TableName() string {\n    return \"user\"\n}\n```\n\n- If you want to use a joint key, you can add a `cache` tag to multiple fields\n\n```go\ntype PublicRelations struct {\n    RelatedId uint64 `cache:\"related_id\"`\n    RelatedType string\n    SourceId uint64 `cache:\"source_id\"`\n    SourceType string\n}\n\nfunc (u PublicRelations) TableName() string {\n    return \"public_relations\"\n}\n```\n\n- Implement the `ICache` interface, you can use redis or gocache as the underlying implementation.\n\n```go\ntype MemoryCacheHandler struct {\n    data map[string][]byte\n}\n\nfunc (m MemoryCacheHandler) StoreAll(keyValues ...gdcache.KeyValue) (err error) {\n    for _, keyValue := range keyValues {\n        m.data[keyValue.Key] = keyValue.Value\n    }\n    return nil\n}\n\nfunc (m MemoryCacheHandler) Get(key string) (data []byte, has bool, err error) {\n    bytes, has := m.data[key]\n    return bytes, has, nil\n}\n\nfunc (m MemoryCacheHandler) GetAll(keys schemas.PK) (data []gdcache.ReturnKeyValue, err error) {\n    returnKeyValues := make([]gdcache.ReturnKeyValue, 0)\n    for _, key := range keys {\n        bytes, has := m.data[key]\n        returnKeyValues = append(returnKeyValues, gdcache.ReturnKeyValue{\n        KeyValue: gdcache.KeyValue{\n        Key:   key,\n        Value: bytes,\n    },\n        Has: has,\n    })\n}\n    return returnKeyValues, nil\n}\n\nfunc (m MemoryCacheHandler) DeleteAll(keys schemas.PK) error {\n    for _, k := range keys {\n        delete(m.data, k)\n    }\n    return nil\n}\n\nfunc NewMemoryCacheHandler() *MemoryCacheHandler {\n    return \u0026MemoryCacheHandler{\n    data: make(map[string][]byte, 0),\n    }\n}\n```\n\n### Gorm usage\n\nImplement the `IDB` interface\n\n```go\ntype GormDB struct {\n    db *gorm.DB\n}\n\nfunc (g GormDB) GetEntries(entries interface{}, sql string) error {\n    tx := g.db.Raw(sql).Find(entries)\n    return tx.Error\n}\n\nfunc (g GormDB) GetEntry(entry interface{}, sql string) (bool, error) {\n    tx := g.db.Raw(sql).Take(entry)\n    if gorm.ErrRecordNotFound == tx.Error {\n        return false, nil\n    }\n    return tx.Error != gorm.ErrRecordNotFound, tx.Error\n}\n\nfunc NewGormCacheHandler() *gdcache.CacheHandler {\n    return gdcache.NewCacheHandler(NewMemoryCacheHandler(), NewGormDd())\n}\n\nfunc NewGormDd() gdcache.IDB {\n    db, err := gorm.Open(mysql.Open(\"root:root@tcp(127.0.0.1:3306)/test?charset=utf8\u0026parseTime=True\u0026loc=Local\"), \u0026gorm.Config{})\n    if err != nil {\n        panic(err)\n    }\n    return GormDB{\n        db: db,\n    }\n}\n```\n\n### Xorm usage\n\nImplement the `IDB` interface\n\n```go\ntype XormDB struct {\n    db *xorm.Engine\n}\n\nfunc (g XormDB) GetEntries(entries interface{}, sql string) ( error) {\n    err := g.db.SQL(sql).Find(entries)\n    return err\n}\n\nfunc (g XormDB) GetEntry(entry interface{}, sql string) ( bool, error) {\n    has, err := g.db.SQL(sql).Get(entry)\n    return has, err\n}\n\nfunc NewXormCacheHandler() *gdcache.CacheHandler {\n    return gdcache.NewCacheHandler(NewMemoryCacheHandler(), NewXormDd())\n}\n\nfunc NewXormDd() gdcache.IDB {\n    db, err := xorm.NewEngine(\"mysql\", \"root:root@/test?charset=utf8\")\n    if err != nil {\n        panic(err)\n    }\n    return XormDB{\n        db: db,\n    }\n}\n```\n\n### Native SQL usage\n\nImplement the `IDB` interface\n\n```go\ntype MemoryDb struct {\n}\n\nfunc NewMemoryDb() *MemoryDb {\n    return \u0026MemoryDb{}\n}\n\nfunc (m MemoryDb) GetEntries(entries interface{}, sql string) error {\n    mockEntries := make([]MockEntry, 0)\n    mockEntries = append(mockEntries, MockEntry{\n    RelateId:   1,\n    SourceId:   2,\n    PropertyId: 3,\n    })\n    marshal, _ := json.Marshal(mockEntries)\n    json.Unmarshal(marshal, entries)\n    return nil\n}\n\nfunc (m MemoryDb) GetEntry(entry interface{}, sql string) (bool, error) {\n    mockEntry := \u0026MockEntry{\n    RelateId:   1,\n    SourceId:   2,\n    PropertyId: 3,\n    }\n    marshal, _ := json.Marshal(mockEntry)\n    json.Unmarshal(marshal, entry)\n    return true, nil\n}\n\nfunc NewMemoryCache() *gdcache.CacheHandler {\n    return gdcache.NewCacheHandler(NewMemoryCacheHandler(), NewMemoryDb())\n}\n```\n\n## How to use\n\nWhen querying a single entity, query through the entity's id and fill it into the entity. When getting multiple\nentities, you can use any sql query and finally fill it into the entity. Both methods must be introduced into the body's\npointer.\n\n```go\nfunc TestNewGormCache(t *testing.T) {\n\n    handler := NewGormCacheHandler()\n\n    user := User{\n        Id: 1,\n    }\n    has, err := handler.GetEntry(\u0026user)\n    if err != nil {\n        t.FailNow()\n    }\n    if has {\n        t.Logf(\"%v\", user)\n    }\n    \n    users := make([]User, 0)\n    err = handler.GetEntries(\u0026users, \"SELECT * FROM user WHERE name = '33'\")\n    if err != nil {\n        t.FailNow()\n    }\n    for _, user := range users {\n        t.Logf(\"%v\", user)\n    }\n    \n    err = handler.GetEntries(\u0026users, \"SELECT * FROM user WHERE id in (3)\")\n    if err != nil {\n        t.FailNow()\n    }\n    for _, user := range users {\n        t.Logf(\"%v\", user)\n    }\n    \n    count, err = handler.GetEntriesAndCount(\u0026users1, \"SELECT * FROM user WHERE id in (1,2)\")\n    if err != nil {\n        t.FailNow()\n    }\n    for _, user := range users1 {\n        t.Logf(\"%v\", user)\n    }\n    t.Log(count)\n    }\n    users3 := make([]User, 0)\n    ids := make([]uint64, 0)\n    count, err = handler.GetEntriesAndCount(\u0026users3, \"SELECT * FROM user WHERE id in ?\", ids)\n    if err != nil {\n        t.FailNow()\n    }\n    for _, user := range users1 {\n        t.Logf(\"%v\", user)\n    }\n    t.Log(count)\n    \n    \n    count, err = handler.GetEntriesAndCount(\u0026users1, \"SELECT * FROM user WHERE id =  ?\", 1)\n    if err != nil {\n        t.FailNow()\n    }\n    for _, user := range users1 {\n        t.Logf(\"%v\", user)\n    }\n    t.Log(count)\n    \n    condition := []User{{Id:   1,},{Id:   2,},{Id:   3,}}\n    \n    err = handler.GetEntriesByIds(\u0026users1, condition)\n    if err != nil {\n        t.FailNow()\n    }\n    for _, user := range users1 {\n        t.Logf(\"%v\", user)\n    }\n    t.Log(count)\n```\n\nSupport placeholder `?`, replacement arrays and basic types\n\n## Contributing\n\nYou can help provide better _gdcahe_ by submitting pr.\n\n## License\n\n© Jovanzhu, 2021~time.Now\n\nReleased under the  [MIT License](https://github.com/ulovecode/gdcache/blob/main/LICENSE)\n","funding_links":[],"categories":["Database","Data Structures","数据库","Data Integration Frameworks","Uncategorized","Generators"],"sub_categories":["Caches","Advanced Console UIs","Standard CLI","缓存"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fulovecode%2Fgdcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fulovecode%2Fgdcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fulovecode%2Fgdcache/lists"}