Skip to content

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

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.

@sonarqubecloud
Copy link

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