{"id":31923836,"url":"https://github.com/arkamfahry/bastioncache-go","last_synced_at":"2026-05-18T18:38:21.959Z","repository":{"id":247431884,"uuid":"825830890","full_name":"arkamfahry/bastioncache-go","owner":"arkamfahry","description":"A robust caching package for Go.","archived":false,"fork":false,"pushed_at":"2025-10-12T05:51:06.000Z","size":142,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-15T07:27:26.065Z","etag":null,"topics":["cache","consistent","distributed","dragonfly","go","keydb","kvrocks","redis","redis-cluster","resilient","valkey"],"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/arkamfahry.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-07-08T15:26:33.000Z","updated_at":"2025-10-12T05:51:09.000Z","dependencies_parsed_at":"2024-07-27T11:50:23.645Z","dependency_job_id":"0f0b1a47-9c05-48a0-992f-e894d04515de","html_url":"https://github.com/arkamfahry/bastioncache-go","commit_stats":null,"previous_names":["driftdev/polycache-go","driftdev/fusecache-go","driftdev/zencache","driftdev/resilicache","driftdev/resilicache-go","driftdev/omnicache","codefrantic/resilicache-go","pixelbend/resilicache-go","pixelbend/supercache-go","arkamfahry/bastioncache-go"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/arkamfahry/bastioncache-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkamfahry%2Fbastioncache-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkamfahry%2Fbastioncache-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkamfahry%2Fbastioncache-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkamfahry%2Fbastioncache-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arkamfahry","download_url":"https://codeload.github.com/arkamfahry/bastioncache-go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkamfahry%2Fbastioncache-go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32983700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-13T11:31:52.688Z","status":"ssl_error","status_checked_at":"2026-05-13T11:31:52.072Z","response_time":115,"last_error":"SSL_read: 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","consistent","distributed","dragonfly","go","keydb","kvrocks","redis","redis-cluster","resilient","valkey"],"created_at":"2025-10-13T23:57:27.603Z","updated_at":"2026-05-13T12:49:17.139Z","avatar_url":"https://github.com/arkamfahry.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BastionCache Go\n\nBastionCache Go is a robust and versatile caching package for Go, compatible with various caching systems, including Redis, RedisCluster, ValKey, KeyDB, DragonflyDB, and Apache Kvrocks. \nIt provides a comprehensive set of features to enhance the reliability and performance of your caching strategy.\n\n## Features\n\n- **Eventual Consistency**: Guarantees eventual consistency of cache data even under extreme conditions, ensuring that your application eventually reflects the most up-to-date information.\n\n- **Strong Consistency**: Offers strong consistency guarantees for cache access, making sure that your application always interacts with the most recent and reliable data.\n\n- **Anti-Breakdown**: Implements strategies to prevent cache breakdown, minimizing the risk of performance degradation during high traffic or system failures.\n\n- **Anti-Penetration**: Provides mechanisms to protect your cache from excessive load caused by cache penetration attacks, where requests bypass the cache and hit the database directly.\n\n- **Anti-Avalanche**: Uses techniques to prevent cache avalanches, which occur when many cache entries expire simultaneously, potentially overwhelming your backend systems.\n\n- **Batch Query**: Supports batch querying to efficiently handle multiple cache requests in a single operation, reducing the overhead and latency of individual cache accesses.\n\n## Installation\n\nTo install SuperCache Go, use the following command:\n\n```bash\ngo get github.com/arkamfahry/bastioncache-go\n```\n\n## Usage\n\nHere's a basic examples of how to use BastionCache Go.\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/arkamfahry/bastioncache-go\"\n\t\"github.com/redis/go-redis/v9\"\n)\n\nfunc main() {\n\t// Initialize Redis client\n\trdb := redis.NewClient(\u0026redis.Options{\n\t\tAddr:     \"localhost:6379\",\n\t\tPassword: \"\",\n\t\tDB:       0,\n\t})\n\n\t// Flush the Redis database to start with a clean state\n\tif err := rdb.FlushDB(context.Background()).Err(); err != nil {\n\t\tlog.Fatalf(\"Error flushing DB: %v\", err)\n\t}\n\n\t// Initialize BastionCache with Redis client and default options\n\tcache := bastioncache.NewCache(rdb, bastioncache.NewDefaultOptions())\n\n\t// Set a key-value pair in the cache with a TTL of 10 seconds\n\terr := cache.Set(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\", []byte(\"tester\"), time.Second*10)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error setting key: %v\", err)\n\t}\n\n\t// Retrieve the value for the key from the cache\n\tvalue, err := cache.Get(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\")\n\tif err != nil {\n\t\tlog.Fatalf(\"Error getting key: %v\", err)\n\t}\n\tif value == nil {\n\t\tlog.Fatalf(\"Error: value not found\")\n\t}\n\tfmt.Println(\"value: \", string(value))\n\n\t// Delete the key from the cache\n\terr = cache.Delete(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\")\n\tif err != nil {\n\t\tlog.Fatalf(\"Error deleting key: %v\", err)\n\t}\n\n\t// Fetch data for the key, caching it if it's not already present\n\t// If the key is not found in the cache, the provided function is called to get the data\n\tvalue, err = cache.FetchSingle(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\", time.Second*10, func() ([]byte, error) {\n\t\t// Fetch data from an external source (e.g., database)\n\t\treturn []byte(\"tester\"), nil\n\t})\n\tif err != nil {\n\t\tlog.Fatalf(\"Error fetching key: %v\", err)\n\t}\n\tfmt.Println(\"value: \", string(value))\n\n\t// Mark the key as deleted, indicating it should not be retrieved from the cache.\n\t// After being marked, the key is permanently deleted with a delay.\n\terr = cache.TagAsDeletedSingle(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\")\n\tif err != nil {\n\t\tlog.Fatalf(\"Error tagging key as deleted: %v\", err)\n\t}\n\n\t// Fetch data for multiple keys, caching them if they are not already present\n\t// If any keys are not found in the cache, the provided function is called to get the data\n\tvalues, err := cache.FetchBatch(context.Background(),\n\t\t[]string{\n\t\t\t\"user:01J61C9MEMNXYKWJXNQ3ERQA1F\",\n\t\t\t\"user:01J61CH3XEVGKHGK3GKA7XJGF9\",\n\t\t\t\"user:01J61CHNPXZ65T9EZ3RSHJJDKV\",\n\t\t},\n\t\ttime.Second*10,\n\t\tfunc(idxs []int) (map[int][]byte, error) {\n\t\t\t// Fetch data from an external source for each key (e.g., database)\n\t\t\tvalues := make(map[int][]byte)\n\t\t\tfor _, i := range idxs {\n\t\t\t\tvalues[i] = []byte(fmt.Sprintf(\"tester_%d\", i))\n\t\t\t}\n\t\t\treturn values, nil\n\t\t},\n\t)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error fetching keys: %v\", err)\n\t}\n\tfor _, v := range values {\n\t\tfmt.Println(\"value: \", string(v))\n\t}\n\n\t// Mark multiple keys as deleted, indicating they should not be retrieved from the cache.\n\t// After being marked, these keys are permanently deleted with a delay.\n\terr = cache.TagAsDeletedBatch(context.Background(),\n\t\t[]string{\n\t\t\t\"user:01J61C9MEMNXYKWJXNQ3ERQA1F\",\n\t\t\t\"user:01J61CH3XEVGKHGK3GKA7XJGF9\",\n\t\t\t\"user:01J61CHNPXZ65T9EZ3RSHJJDKV\",\n\t\t},\n\t)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error tagging keys as deleted: %v\", err)\n\t}\n}\n```\n\nHere's a basic examples of how to use BastionCache TypedCache.\nThis leverages Go generics to enforce type safety, ensuring that only \nspecified types are stored and retried.\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/arkamfahry/bastioncache-go\"\n\t\"github.com/redis/go-redis/v9\"\n)\n\nfunc main() {\n\t// Initialize Redis client\n\trdb := redis.NewClient(\u0026redis.Options{\n\t\tAddr:     \"localhost:6379\",\n\t\tPassword: \"\",\n\t\tDB:       0,\n\t})\n\n\t// Flush the Redis database to start with a clean state\n\tif err := rdb.FlushDB(context.Background()).Err(); err != nil {\n\t\tlog.Fatalf(\"Error flushing DB: %v\", err)\n\t}\n\n\t// Initialize BastionCache with Redis client and default options\n\tcache := bastioncache.NewCache(rdb, bastioncache.NewDefaultOptions())\n\n\t// Initialize a TypedCache instance with string as the value type\n\t// This enforces type safety, ensuring that only strings are stored and retrieved from the cache\n\ttypedCache := bastioncache.NewTypedCache[string](cache)\n\n\t// Set a key-value pair in the cache with a TTL of 10 seconds\n\terr := typedCache.Set(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\", \"tester\", time.Second*10)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error setting key: %v\", err)\n\t}\n\n\t// Retrieve the value for the key from the cache\n\t// The retrieved value is already typed as a string\n\tvalue, err := typedCache.Get(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\")\n\tif err != nil {\n\t\tlog.Fatalf(\"Error getting key: %v\", err)\n\t}\n\tif value == \"\" {\n\t\tlog.Fatalf(\"Error: value not found\")\n\t}\n\tfmt.Println(\"value: \", value)\n\n\t// Delete the key from the cache\n\terr = typedCache.Delete(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\")\n\tif err != nil {\n\t\tlog.Fatalf(\"Error deleting key: %v\", err)\n\t}\n\n\t// Fetch data for the key, caching it if it's not already present\n\t// If the key is not found in the cache, the provided function is called to generate the data\n\tvalue, err = typedCache.FetchSingle(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\", time.Second*10, func() (string, error) {\n\t\t// Fetch or generate data (e.g., from a database)\n\t\treturn \"tester\", nil\n\t})\n\tif err != nil {\n\t\tlog.Fatalf(\"Error fetching key: %v\", err)\n\t}\n\tfmt.Println(\"value: \", value)\n\n\t// Mark the key as deleted, so it should not be retrieved from the cache\n\t// The key is permanently deleted after a delay\n\terr = typedCache.TagAsDeletedSingle(context.Background(), \"user:01J61BPPHMFH9VSF2T1R2ZXDA2\")\n\tif err != nil {\n\t\tlog.Fatalf(\"Error tagging key as deleted: %v\", err)\n\t}\n\n\t// Fetch data for multiple keys, caching them if not already present\n\t// If any keys are missing from the cache, the provided function is called to generate the data\n\tvalues, err := typedCache.FetchBatch(context.Background(),\n\t\t[]string{\n\t\t\t\"user:01J61C9MEMNXYKWJXNQ3ERQA1F\",\n\t\t\t\"user:01J61CH3XEVGKHGK3GKA7XJGF9\",\n\t\t\t\"user:01J61CHNPXZ65T9EZ3RSHJJDKV\",\n\t\t},\n\t\ttime.Second*10,\n\t\tfunc(idxs []int) (map[int]string, error) {\n\t\t\t// Fetch or generate data for each key (e.g., from a database)\n\t\t\tvalues := make(map[int]string)\n\t\t\tfor _, i := range idxs {\n\t\t\t\tvalues[i] = fmt.Sprintf(\"tester_%d\", i)\n\t\t\t}\n\t\t\treturn values, nil\n\t\t},\n\t)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error fetching keys: %v\", err)\n\t}\n\tfor _, v := range values {\n\t\tfmt.Println(\"value: \", v)\n\t}\n\n\t// Mark multiple keys as deleted, so they should not be retrieved from the cache\n\t// The keys are permanently deleted after a delay\n\terr = typedCache.TagAsDeletedBatch(context.Background(),\n\t\t[]string{\n\t\t\t\"user:01J61C9MEMNXYKWJXNQ3ERQA1F\",\n\t\t\t\"user:01J61CH3XEVGKHGK3GKA7XJGF9\",\n\t\t\t\"user:01J61CHNPXZ65T9EZ3RSHJJDKV\",\n\t\t},\n\t)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error tagging keys as deleted: %v\", err)\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farkamfahry%2Fbastioncache-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farkamfahry%2Fbastioncache-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farkamfahry%2Fbastioncache-go/lists"}