Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cleanup block to FastCache::Cache, with RSpec tests. #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

joelnordell
Copy link
Contributor

This allows the user to specify a block to be called whenever an object is removed from the cache, to perform some cleanup action on it:

Example usage scenario -- caching connections that should be closed upon removal from the cache:

  cache = FastCache::Cache.new do |conn|
    conn.close
  end

  cache.fetch('/some/connection') do
    Connection.new
  end

@@ -39,6 +39,7 @@ def initialize(max_size, ttl, expire_interval = 100)
@op_count = 0
@data = {}
@expires_at = {}
@cleanup = Proc.new if block_given?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide the block as an &param. This type of defaulting is one of the obscure features of Ruby that saves a few characters at the expense of readability.

@ssimeonov
Copy link
Contributor

A high level question about this PR: does it make sense to add the cleanup behavior to a simple fast cache? If the use case is to cache connections, of which there typically are not many, and which do slow I/O, does it matter whether the cache is fast or slow?

@joelnordell
Copy link
Contributor Author

Good comments -- I'll push another commit that addresses them.

Regarding the high level question: my use case is actually not caching slow things like connections, but caching allocated data structures for a native library (via FFI) which contain open file descriptors that need to be manually cleared/closed.

I'm using FastCache not only for its speed, but also for its convenient API and the ability to specify both a maximum size and a time-to-live.

Copy link

sonarcloud bot commented Jun 28, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants