{"id":25842748,"url":"https://github.com/nir3x/dbcachinglayer","last_synced_at":"2026-05-13T04:37:02.934Z","repository":{"id":274981653,"uuid":"924698585","full_name":"NIR3X/dbcachinglayer","owner":"NIR3X","description":"DB Caching Layer Module - A Go-based generic, thread-safe caching and persistence layer for database records","archived":false,"fork":false,"pushed_at":"2025-01-30T14:01:44.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T14:35:29.648Z","etag":null,"topics":["caching","concurrency","crud","database","generic","go","layer","persistence","sql","thread-safe"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NIR3X.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":"2025-01-30T13:46:24.000Z","updated_at":"2025-01-30T14:01:48.000Z","dependencies_parsed_at":"2025-01-30T14:46:34.888Z","dependency_job_id":null,"html_url":"https://github.com/NIR3X/dbcachinglayer","commit_stats":null,"previous_names":["nir3x/dbcachinglayer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NIR3X%2Fdbcachinglayer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NIR3X%2Fdbcachinglayer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NIR3X%2Fdbcachinglayer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NIR3X%2Fdbcachinglayer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NIR3X","download_url":"https://codeload.github.com/NIR3X/dbcachinglayer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241329448,"owners_count":19944982,"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":["caching","concurrency","crud","database","generic","go","layer","persistence","sql","thread-safe"],"created_at":"2025-03-01T06:31:33.771Z","updated_at":"2026-05-13T04:37:02.905Z","avatar_url":"https://github.com/NIR3X.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DB Caching Layer Module - A Go-based generic, thread-safe caching and persistence layer for database records\n\n## Overview\nThe `DBCL` (Database Caching Layer) module is a generic, thread-safe caching and persistence layer for database records in Go. It supports real-time caching of records and scheduled synchronization with a SQL database backend.\n\nThis implementation enables developers to manage records efficiently while abstracting typical CRUD operations and caching mechanisms.\n\n---\n\n## Features\n\n- **Generic Record Handling:** Supports any custom record type by implementing the `DBCLRecord` interface.\n- **Automatic Synchronization:** Periodically saves cached records to the database.\n- **In-Memory Record Cache:** Reduces database queries by maintaining an in-memory cache.\n- **Transactional Updates:** Ensures data consistency through SQL transactions.\n- **Thread-Safe Operations:** Concurrent-safe access to records.\n- **By-Columns Record Indexing:** Efficient retrieval of records using custom column values as lookup keys.\n\n---\n\n## Installation\n\n```bash\ngo get github.com/NIR3X/dbcachinglayer\n```\n\n---\n\n## Usage\n\n### 1. Define Your Record Type\nImplement the `DBCLRecord` interface for your custom record type.\n\n```go\npackage main\n\ntype Note struct {\n\tId int64\n\tTitle string\n\tContent string\n}\n\nfunc NewNote(id int64, title, content string) *Note {\n\treturn \u0026Note{id, title, content}\n}\n\nfunc (n *Note) DBCLClone() DBCLRecord {\n\treturn NewNote(n.Id, n.Title, n.Content)\n}\n\n// Implement other DBCLRecord methods...\n```\n\n### 2. Initialize the DB Caching Layer\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n\t\"log\"\n\t\"time\"\n\n\t_ \"github.com/mattn/go-sqlite3\"\n)\n\nfunc main() {\n\tdbcl, err := NewDBCL[*Note](\"sqlite3\", \"notes.db\", 15*time.Second, nil)\n\tif err != nil {\n\t\tlog.Fatalf(\"Failed to create DBCL: %v\", err)\n\t}\n\tdefer dbcl.Close()\n\n\tnote := \u0026Note{Title: \"Sample\", Content: \"Sample Content\"}\n\tdbcl.InsertRecord(note)\n}\n```\n\n### 3. CRUD Operations\n\n- **Insert:**\n```go\ndbcl.InsertRecord(note)\n```\n\n- **Get Record by ID:**\n```go\nrecord := dbcl.GetRecord(note.Id)\n```\n\n- **Update:**\n```go\nnote.Title = \"Updated Title\"\ndbcl.UpdateRecord(note.Id, note)\n```\n\n- **Delete:**\n```go\ndbcl.DeleteRecord(note.Id)\n```\n\n---\n\n## By-Columns Feature\n\nThe `byColumns` feature is a powerful way to maintain an additional in-memory lookup table for efficient record retrieval based on specific column values (such as `title`). This helps avoid full scans and speeds up searches when using frequently accessed columns as indexes.\n\nYou can define a custom modify callback function to track changes in specific columns:\n\n```go\ndbcl, err := NewDBCL[*Note](\"sqlite3\", \":memory:\", 15*time.Second, func(byColumns map[string]interface{}, id int64, oldRecord, newRecord *Note) {\n\tbyTitle, ok := byColumns[\"title\"].(map[string]*Note)\n\tif !ok {\n\t\tbyTitle = make(map[string]*Note)\n\t\tbyColumns[\"title\"] = byTitle\n\t}\n\tif oldRecord != nil {\n\t\tdelete(byTitle, oldRecord.Title)\n\t}\n\tif newRecord != nil {\n\t\tbyTitle[newRecord.Title] = newRecord\n\t}\n})\n```\n\n### Retrieve Record by Column\n\nTo efficiently get a record by a custom column value (e.g., title), use `GetRecordByColumn`:\n\n```go\nrecord := GetRecordByColumn(dbcl, \"title\", \"Sample Title\")\nif record != nil {\n\tlog.Printf(\"Found record: %+v\\n\", record)\n} else {\n\tlog.Println(\"Record not found\")\n}\n```\n\n---\n\n## Running Tests\n\nThe module includes unit tests for key operations.\n\n```bash\ngo test ./...\n```\n\n---\n\n## Example Test Output\n\n```bash\n=== RUN TestDBCachingLayer\n--- PASS: TestDBCachingLayer (0.02s)\nPASS\nok dbcachinglayer 0.021s\n```\n\n---\n\n## License\n\n[![GNU AGPLv3 Image](https://www.gnu.org/graphics/agplv3-155x51.png)](https://www.gnu.org/licenses/agpl-3.0.html)\n\nThis program is Free Software: You can use, study share and improve it at your\nwill. Specifically you can redistribute and/or modify it under the terms of the\n[GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html) as\npublished by the Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnir3x%2Fdbcachinglayer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnir3x%2Fdbcachinglayer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnir3x%2Fdbcachinglayer/lists"}