{"id":13411822,"url":"https://github.com/rocketlaunchr/remember-go","last_synced_at":"2025-04-12T19:32:33.652Z","repository":{"id":45555852,"uuid":"179571623","full_name":"rocketlaunchr/remember-go","owner":"rocketlaunchr","description":"Cache Slow Database Queries","archived":false,"fork":false,"pushed_at":"2021-04-19T07:43:10.000Z","size":133,"stargazers_count":143,"open_issues_count":2,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-05T06:55:33.931Z","etag":null,"topics":["cache","database","go","golang","memoisation","memoization","queries","redis"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rocketlaunchr.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":"2019-04-04T20:24:25.000Z","updated_at":"2025-04-04T21:56:42.000Z","dependencies_parsed_at":"2022-07-16T02:00:34.556Z","dependency_job_id":null,"html_url":"https://github.com/rocketlaunchr/remember-go","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocketlaunchr%2Fremember-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocketlaunchr%2Fremember-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocketlaunchr%2Fremember-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocketlaunchr%2Fremember-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rocketlaunchr","download_url":"https://codeload.github.com/rocketlaunchr/remember-go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248621334,"owners_count":21134814,"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","database","go","golang","memoisation","memoization","queries","redis"],"created_at":"2024-07-30T20:01:17.267Z","updated_at":"2025-04-12T19:32:33.393Z","avatar_url":"https://github.com/rocketlaunchr.png","language":"Go","readme":"\u003cp align=\"right\"\u003e\n  ⭐ \u0026nbsp;\u0026nbsp;\u003cstrong\u003ethe project to show your appreciation.\u003c/strong\u003e :arrow_upper_right:\n\u003c/p\u003e\n\n\u003cp align=\"right\"\u003e\n  \u003ca href=\"http://godoc.org/github.com/rocketlaunchr/remember-go\"\u003e\u003cimg src=\"http://godoc.org/github.com/rocketlaunchr/remember-go?status.svg\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://goreportcard.com/report/github.com/rocketlaunchr/remember-go\"\u003e\u003cimg src=\"https://goreportcard.com/badge/github.com/rocketlaunchr/remember-go\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://gocover.io/github.com/rocketlaunchr/remember-go\"\u003e\u003cimg src=\"http://gocover.io/_badge/github.com/rocketlaunchr/remember-go\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://github.com/rocketlaunchr/remember-go/raw/master/assets/logo.png\" alt=\"dataframe-go\" /\u003e\n\u003c/p\u003e\n\n# Cache Slow Database Queries\n\nThis package is used to cache the results of slow database queries in memory or Redis.\nIt can be used to cache any form of data (eg. function memoization). A Redis and in-memory storage driver is provided.\n\nSee [Article](https://medium.com/@rocketlaunchr.cloud/caching-slow-database-queries-1085d308a0c9) for further details including a tutorial.\n\nThe package is **production ready** and the API is stable. A variant of this package has been used in production for over 4 years.\n\n## Installation\n\n```\ngo get -u github.com/rocketlaunchr/remember-go\n```\n\n## Create a Key\n\nLet’s assume the query’s argument is an arbitrary `search` term and a `page` number for pagination.\n\n### CreateKeyStruct\n\nCreateKeyStruct can generate a JSON based key by providing a struct.\n\n```go\ntype Key struct {\n    Search string\n    Page   int `json:\"page\"`\n}\n\nvar key string = remember.CreateKeyStruct(Key{\"golang\", 2})\n```\n\n### CreateKey\n\nCreateKey provides more flexibility to generate keys:\n\n```go\n// Key will be \"search-golang-2\"\nkey :=  remember.CreateKey(false, \"-\", \"search-x-y\", \"search\", \"golang\", 2)\n```\n\n## Initialize the Storage Driver\n\n### In-Memory\n\n```go\nimport \"github.com/rocketlaunchr/remember-go/memory\"\n\nvar ms = memory.NewMemoryStore(10 * time.Minute)\n```\n\n### Redis\n\nThe Redis storage driver relies on Gary Burd’s excellent [Redis client library](https://github.com/gomodule/redigo/).\n\n```go\nimport red \"github.com/rocketlaunchr/remember-go/redis\"\nimport \"github.com/gomodule/redigo/redis\"\n\nvar rs = red.NewRedisStore(\u0026redis.Pool{\n    Dial: func() (redis.Conn, error) {\n        return redis.Dial(\"tcp\", \"localhost:6379\")\n    },\n})\n```\n\n### Memcached\n\nAn experimental (and untested) memcached driver is provided.\nIt relies on Brad Fitzpatrick's [memcache driver](https://godoc.org/github.com/bradfitz/gomemcache/memcache).\n\n### Ristretto\n\nDGraph's [Ristretto](https://github.com/dgraph-io/ristretto) is a fast, fixed size, in-memory cache with a dual focus on throughput and hit ratio performance.\n\n### Nocache\n\nThis driver is for testing purposes. It does not cache any data.\n\n## Create a SlowRetrieve Function\n\nThe package initially checks if data exists in the cache. If it doesn’t, then it elegantly fetches the data directly from the database by calling the `SlowRetrieve` function. It then saves the data into the cache so that next time it doesn’t have to refetch it from the database.\n\n```go\ntype Result struct {\n    Title string\n}\n\nslowQuery := func(ctx context.Context) (interface{}, error) {\n    results := []Result{}\n\n    stmt := `\n        SELECT title\n        FROM books WHERE title LIKE ?\n        ORDER BY title LIMIT ?, 20\n    `\n\n    rows, _ := db.QueryContext(ctx, stmt, search, (page-1)*20)\n\n    for rows.Next() {\n        var title string\n        rows.Scan(\u0026title)\n        results = append(results, Result{title})\n    }\n\n    return results, nil\n}\n```\n\n## Usage\n\n```go\nkey := remember.CreateKeyStruct(Key{\"golang\", 2})\nexp := 10*time.Minute\n\nresults, found, err := remember.Cache(ctx, ms, key, exp, slowQuery, remember.Options{GobRegister: false})\n\nreturn results.([]Result) // Type assert in order to use\n```\n\n## Gob Register Errors\n\nThe Redis storage driver stores the data in a `gob` encoded form. You have to register with the [`gob`](https://golang.org/pkg/encoding/gob/) package the data type returned by the `SlowRetrieve` function. It can be done inside a `func init()`. Alternatively, you can set the `GobRegister` option to true. This will impact concurrency performance and is thus **not recommended**.\n\n## Other useful packages\n\n- [awesome-svelte](https://github.com/rocketlaunchr/awesome-svelte) - Resources for killing react\n- [dataframe-go](https://github.com/rocketlaunchr/dataframe-go) - For statistics, machine-learning, and data manipulation/exploration\n- [dbq](https://github.com/rocketlaunchr/dbq) - Zero boilerplate database operations for Go\n- [electron-alert](https://github.com/rocketlaunchr/electron-alert) - SweetAlert2 for Electron Applications\n- [google-search](https://github.com/rocketlaunchr/google-search) - Scrape google search results\n- [igo](https://github.com/rocketlaunchr/igo) - A Go transpiler with cool new syntax such as fordefer (defer for for-loops)\n- [mysql-go](https://github.com/rocketlaunchr/mysql-go) - Properly cancel slow MySQL queries\n- [react](https://github.com/rocketlaunchr/react) - Build front end applications using Go\n- [testing-go](https://github.com/rocketlaunchr/testing-go) - Testing framework for unit testing\n\n#\n\n### Legal Information\n\nThe license is a modified MIT license. Refer to `LICENSE` file for more details.\n\n**© 2019-21 PJ Engineering and Business Solutions Pty. Ltd.**\n\n### Final Notes\n\nFeel free to enhance features by issuing pull-requests.\n","funding_links":[],"categories":["Database","Generators","Data Structures","数据库","Uncategorized","数据结构`go语言实现的数据结构与算法`","数据结构"],"sub_categories":["Caches","Advanced Console UIs","缓存","标准 CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocketlaunchr%2Fremember-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frocketlaunchr%2Fremember-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocketlaunchr%2Fremember-go/lists"}