{"id":23062329,"url":"https://github.com/elara6331/chaistore","last_synced_at":"2025-04-03T07:26:45.873Z","repository":{"id":224452503,"uuid":"763279633","full_name":"Elara6331/chaistore","owner":"Elara6331","description":"A Chai-based session store for SCS","archived":false,"fork":false,"pushed_at":"2024-02-26T02:34:26.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-08T21:16:18.490Z","etag":null,"topics":["go","golang","session","session-store","sessions"],"latest_commit_sha":null,"homepage":"https://gitea.elara.ws/Elara6331/chaistore","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/Elara6331.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}},"created_at":"2024-02-26T01:35:44.000Z","updated_at":"2024-02-26T02:31:55.000Z","dependencies_parsed_at":"2024-02-26T03:46:10.977Z","dependency_job_id":null,"html_url":"https://github.com/Elara6331/chaistore","commit_stats":null,"previous_names":["elara6331/chaistore"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elara6331%2Fchaistore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elara6331%2Fchaistore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elara6331%2Fchaistore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elara6331%2Fchaistore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Elara6331","download_url":"https://codeload.github.com/Elara6331/chaistore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246955074,"owners_count":20860208,"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":["go","golang","session","session-store","sessions"],"created_at":"2024-12-16T03:26:41.426Z","updated_at":"2025-04-03T07:26:45.846Z","avatar_url":"https://github.com/Elara6331.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chaistore\n\n[![Go Reference](https://pkg.go.dev/badge/go.elara.ws/chaistore.svg)](https://pkg.go.dev/go.elara.ws/chaistore)\n\nA [Chai](https://github.com/chaisql/chai)-based session store for [SCS](https://github.com/alexedwards/scs).\n\n## Setup\n\nYou should have a working Chai database file containing a `sessions` table with the definition:\n\n```sql\nCREATE TABLE sessions (\n\ttoken  TEXT      PRIMARY KEY,\n\tdata   BLOB      NOT NULL,\n\texpiry TIMESTAMP NOT NULL\n);\n\nCREATE INDEX idx_sessions_expiry ON sessions(expiry);\n```\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n\t\"io\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/alexedwards/scs/v2\"\n\t\"go.elara.ws/chaistore\"\n\t\"github.com/chaisql/chai\"\n)\n\nvar sessionManager *scs.SessionManager\n\nfunc main() {\n\t// Open a Chai database.\n\tdb, err := chai.Open(\"chai_db\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer db.Close()\n\n\t// Initialize a new session manager and configure it to use chaistore as the session store.\n\tsessionManager = scs.New()\n\tsessionManager.Store = chaistore.New(db)\n\n\tmux := http.NewServeMux()\n\tmux.HandleFunc(\"/put\", putHandler)\n\tmux.HandleFunc(\"/get\", getHandler)\n\n\thttp.ListenAndServe(\":4000\", sessionManager.LoadAndSave(mux))\n}\n\nfunc putHandler(w http.ResponseWriter, r *http.Request) {\n\tsessionManager.Put(r.Context(), \"message\", \"Hello from a session!\")\n}\n\nfunc getHandler(w http.ResponseWriter, r *http.Request) {\n\tmsg := sessionManager.GetString(r.Context(), \"message\")\n\tio.WriteString(w, msg)\n}\n```\n\n## Expired Session Cleanup\n\nThis package provides a background 'cleanup' goroutine to delete expired session data. This stops the database table from holding on to invalid sessions indefinitely and growing unnecessarily large. By default the cleanup runs every 5 minutes. You can change this by using the `NewWithCleanupInterval()` function to initialize your session store. For example:\n\n```go\n// Run a cleanup every 30 minutes.\nchaistore.NewWithCleanupInterval(db, 30*time.Minute)\n\n// Disable the cleanup goroutine by setting the cleanup interval to zero.\nchaistore.NewWithCleanupInterval(db, 0)\n```\n\n### Terminating the Cleanup Goroutine\n\nIt's rare that the cleanup goroutine needs to be terminated --- it is generally intended to be long-lived and run for the lifetime of your application.\n\nHowever, there may be occasions when your use of a session store instance is transient. A common example would be using it in a short-lived test function. In this scenario, the cleanup goroutine (which will run forever) will prevent the session store instance from being garbage collected even after the test function has finished. You can prevent this by either disabling the cleanup goroutine altogether (as described above) or by stopping it using the `StopCleanup()` method. For example:\n\n```go\nfunc TestExample(t *testing.T) {\n\tdb, err := chai.Open(\"chai_db\")\n\tif err != nil {\n\t    t.Fatal(err)\n\t}\n\tdefer db.Close()\n\n\tstore := chaistore.New(db)\n\tdefer store.StopCleanup()\n\n\tsessionManager = scs.New()\n\tsessionManager.Store = store\n\n\t// Run test...\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felara6331%2Fchaistore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felara6331%2Fchaistore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felara6331%2Fchaistore/lists"}