New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

activerecord_cached

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

activerecord_cached

  • 1.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

ActiveRecordCached

This gem adds methods to cache small amounts of ActiveRecord data in RAM, Redis, or other stores.

The cache for each model is busted both by individual CRUD operations on that model (e.g. #update), as well as by mass operations (e.g. #update_all).

The cached methods work on whole models as well as on relations.

There is an automatic warning if the data is getting too big to cache.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add activerecord_cached

Usage

Pizza.cached_count # => 2374
Pizza.limit(2).cached_pluck(:name) # => ["Funghi", "Spinaci"]
Pizza.select(:id, :name).cached_records # => [#<Pizza id=1 name="Funghi">, ...]

Configuration:

ActiveRecordCached.configure do |config|
  # How to cache the data. Default: MemoryStore
  config.cache_store = Rails.cache

  # Maximum size of the standard memory store. Default: 32MB
  config.max_total_bytes = 16.megabytes

  # How much data to allow per cached relation. Default: 1MB
  config.max_bytes = 2.megabytes

  # How many records to allow per cached relation. Default: 10k
  config.max_count = 1000

  # What do to when any limit is exceeded. Default: warn
  config.on_limit_reached = ->msg{ report(msg) }
end

Specs:

RSpec.configure do |config|
  config.after(:each) { ActiveRecordCached.clear_all }
end

Tradeoffs

The default MemoryStore is orders of magnitude faster than e.g. Redis, but it is wiped when the app is deployed or otherwise restarted, which may present a cache stampede risk under very high load.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jaynetics/activerecord_cached.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 25 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc