{"id":13878755,"url":"https://github.com/mrsool/redis_hash_store","last_synced_at":"2025-07-16T14:32:55.994Z","repository":{"id":50128516,"uuid":"292372160","full_name":"mrsool/redis_hash_store","owner":"mrsool","description":"RedisHashStore extends ActiveSupport's RedisCacheStore to provide the ability to easily use Redis hashes for caching.","archived":false,"fork":false,"pushed_at":"2023-03-21T16:34:56.000Z","size":79,"stargazers_count":22,"open_issues_count":1,"forks_count":5,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-08-07T08:11:39.886Z","etag":null,"topics":["cache","caching","rails","redis","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/mrsool.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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}},"created_at":"2020-09-02T19:10:51.000Z","updated_at":"2024-07-04T09:54:01.000Z","dependencies_parsed_at":"2024-01-13T20:49:25.585Z","dependency_job_id":null,"html_url":"https://github.com/mrsool/redis_hash_store","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrsool%2Fredis_hash_store","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrsool%2Fredis_hash_store/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrsool%2Fredis_hash_store/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrsool%2Fredis_hash_store/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrsool","download_url":"https://codeload.github.com/mrsool/redis_hash_store/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226138849,"owners_count":17579496,"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","caching","rails","redis","ruby"],"created_at":"2024-08-06T08:01:58.896Z","updated_at":"2024-11-24T07:31:18.082Z","avatar_url":"https://github.com/mrsool.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# RedisHashStore\n![](https://img.shields.io/gem/v/redis_hash_store) ![](https://img.shields.io/github/workflow/status/mrsool/redis_hash_store/CI)\n\n`RedisHashStore` extends ActiveSupport's [`RedisCacheStore`](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/cache/redis_cache_store.rb) to provide the ability to easily use Redis hashes for caching.\n\nWe decided to create this gem because:\n\n1. We were previously using [`#delete_matched`](https://apidock.com/rails/ActiveSupport/Cache/Store/delete_matched) which can have many performance issues at scale (See this [similar issue at GitLab](https://gitlab.com/gitlab-org/gitlab/-/issues/201808)).\n2. `#deleted_matched` doesn't delete values from all the nodes in a Redis cluster.\n\n## Rails\nSupported Rails versions are listed in [`Appraisals`](https://github.com/mrsool/redis_hash_store/blob/master/Appraisals). \n\n## Installing\nInstall it yourself as:\n```bash\n$ gem install redis_hash_store\n```\nOr add it to your `Gemfile`:\n```ruby\ngem 'redis_hash_store'\n```\nand then execute:\n```bash\n$ bundle\n```\n\n## Configuration\nAll you need to do is add:\n```ruby\n# config/production(development|test|staging|preview).rb\n\nconfig.cache_store = :redis_hash_store, redis_cache_store_options\n```\n\n## Usage\nThis gem simply adds new functionality to `RedisCacheStore`, so all existing logic in that class is not affected.\n\nHere is a list of available methods:\n\n* [`#write_hash_value`](#write_hash_value)\n* [`#read_hash_value`](#read_hash_value)\n* [`#read_hash`](#read_hash)\n* [`#delete_hash_value`](#delete_hash_value)\n* [`#delete_hash`](#delete_hash)\n* [`#fetch_hash_value`](#fetch_hash_value)\n\n## Examples\n\n\u003e Let's imagine we need to store amount of Services for City.\n\n#### #write_hash_value\n\n```ruby\ncity = \"Riyadh\"\ncoffee_shop_type = \"coffee_shop\"\nrestaurants_type = \"restaurant\"\n\ncoffee_shops_count = Service.where(type: coffee_shop_type, city: city).count\n=\u003e 250\nrestaurants_count = Service.where(type: restaurants_type, city: city).count\n=\u003e 340\n\nRails.cache.write_hash_value(\"#{city} counters\", coffee_shop_type, coffee_shops_count)\n=\u003e 1\nRails.cache.write_hash_value(\"#{city} counters\", restaurants_type, restaurants_count)\n=\u003e 1\n```\n#### #read_hash_value\n\nNow it's accessible by:\n\n```ruby\nRails.cache.read_hash_value(\"#{city} counters\", coffee_shop_type)\n=\u003e 250\nRails.cache.read_hash_value(\"#{city} counters\", restaurants_type)\n=\u003e 340\n```\n#### #read_hash\n\nLooks pretty easy, right? Maybe you're thinking: \"What the difference?\"\n\n1. You can access all records under the `\"#{city} counters\"` hash\n```ruby\nRails.cache.read_hash(\"#{city} counters\")\n=\u003e { \"coffee_shop\"=\u003e250, \"restaurant\"=\u003e340 }\n```\n\n#### #delete_hash_value\n\n2. You can easily remove every value under `\"#{city} counters\"`\n```ruby\nRails.cache.delete_hash_value(\"#{city} counters\", coffee_shop_type)\n=\u003e 1\n```\n\n#### #delete_hash\n\n3. You can also delete the entire `\"#{city} counters\"` hash\n```ruby\nRails.cache.delete_hash(\"#{city} counters\")\n=\u003e 1\n```\n#### #fetch_hash_value\n4. You can fetch needed value under `\"#{city} counters\"`\n```ruby\nRails.cache.fetch_hash_value(\"#{city} counters\", coffee_shop_type) do\n  Service.where(type: coffee_shop_type, city: city).count\nend\n=\u003e 250\n\nRails.cache.fetch_hash_value(\"#{city} counters\", coffee_shop_type, force: true) do\n  Service.where(type: coffee_shop_type, city: city).count * 2\nend\n=\u003e 500\n```\n\n## Benchmarks\n```ruby\nindexes = 1..1_000_000\n\nindexes.each do |index|\n  Rails.cache.write(\"some_data_#{index}\", index)\n  Rails.cache.write_hash_value(\"some_data\", index, index)\nend\n\nBenchmark.bm do |x|\n  x.report(\"delete_matched\")  { Rails.cache.delete_matched(\"some_data_*\") }\n  x.report(\"delete_hash\")     { Rails.cache.delete_hash(\"some_data\") }\nend\n\n    user         system      total        real\ndelete_matched  0.571040   0.244962   0.816002 (3.791056)\ndelete_hash     0.000000   0.000225   0.000225 (0.677891)\n```\n\n## Contributing\nPlease see [CONTRIBUTING.md](https://github.com/mrsool/redis_hash_store/blob/master/CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrsool%2Fredis_hash_store","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrsool%2Fredis_hash_store","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrsool%2Fredis_hash_store/lists"}