{"id":18418545,"url":"https://github.com/bsm/activesupport-cache-database","last_synced_at":"2025-04-12T14:14:03.337Z","repository":{"id":50144352,"uuid":"207815694","full_name":"bsm/activesupport-cache-database","owner":"bsm","description":"ActiveSupport::Cache::Store implementation backed by a database via ActiveRecord","archived":false,"fork":false,"pushed_at":"2024-08-02T01:41:15.000Z","size":55,"stargazers_count":28,"open_issues_count":5,"forks_count":9,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-08T20:06:08.047Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bsm.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2019-09-11T13:10:31.000Z","updated_at":"2024-05-13T00:27:45.000Z","dependencies_parsed_at":"2024-12-16T04:06:50.602Z","dependency_job_id":"4bd86ad7-98d4-475f-9d56-464db26aad22","html_url":"https://github.com/bsm/activesupport-cache-database","commit_stats":{"total_commits":15,"total_committers":5,"mean_commits":3.0,"dds":0.2666666666666667,"last_synced_commit":"ee70859fceaabcbb78f72d3d8b0faac4463f2667"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Factivesupport-cache-database","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Factivesupport-cache-database/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Factivesupport-cache-database/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Factivesupport-cache-database/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bsm","download_url":"https://codeload.github.com/bsm/activesupport-cache-database/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248578854,"owners_count":21127713,"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":[],"created_at":"2024-11-06T04:13:56.856Z","updated_at":"2025-04-12T14:14:03.315Z","avatar_url":"https://github.com/bsm.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ActiveSupport::Cache::DatabaseStore\n\n[![Test](https://github.com/bsm/activesupport-cache-database/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/activesupport-cache-database/actions/workflows/test.yml)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\nActiveSupport::Cache::Store implementation backed by a database via ActiveRecord.\n\nTested with:\n\n- PostgreSQL\n- SQlite3\n- MySQL/MariaDB\n\n## Install\nAdd gem to Gemfile and bundle install.\n\n```gem 'activesupport-cache-database'```\n\nThis gem requires a database table `activesupport_cache_entries` to be created. To do so generate a migration that would create required table.\n\n```rails generate cache:database:install```\n\nMake sure to read through migration file, before running a migration. You might want to tweak it to fit your usecase.\n\n`rails db:migrate`\n\n## Usage\n\nOpen and use the new cache instance:\n```ruby\ncache = ActiveSupport::Cache::DatabaseStore.new namespace: 'my-scope'\nvalue = cache.fetch('some-key') { 'default' }\n```\n\nTo use as a Rails cache store, simply use a new instance.\n\n```ruby\nconfig.cache_store = ActiveSupport::Cache::DatabaseStore.new\n```\n\n## Variable Compression\nBy default this gem doesn't use any compression to store records, but there is an option to use `gzip` by providing a \"compression\" paramater. e.g\n\n\n```\ncache(project, compression: 'gzip') do\n  {code: 'to cache'}\nend\n```\n\nThere are some cases, when compression could be skipped - if value is numeric (e.g. increment/decrement counters) and when value is less than 1024 bytes. This is done to avoid unreasonable overhead on performance.\n\n## Maintenance\nAfter you have started caching into the database, you will likely see the database size growing significantly. It is crucial to implement an effective strategy to evict the cache from your DB.\n\nThere may be a large number of cache entries that do not possess an `expires_at` value, so it will be necessary to decide on an optimal timeframe for storing your cache.\n\nThis next piece of code should be run periodically:\n```\nActiveSupport::Cache::DatabaseStore.new.cleanup(\n  created_before: 1.week.ago\n)\n```\nWithout providing a `created_before` value, only those caches with `expires_at` values will be cleaned, leaving behind plenty of dead cache.\n\nIf you're using PostgreSQL, consider running vacuum or pg_repack intermittently to delete data physically as well.\n\n\n## Warning\nThere are two things you need to be aware about while using this gem:\n- For performance reasons, your database may not be the most suitable general purpose cache backend. But in some cases, caching complex quieries in cache could be a good enough improvement.\n- While already generally usable as a Rails cache store, this gem doesn't yet implement all required methods.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Factivesupport-cache-database","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbsm%2Factivesupport-cache-database","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Factivesupport-cache-database/lists"}