Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/freshworks/memoize_until
This gem is an extension to the standard memoization pattern.
https://github.com/freshworks/memoize_until
cache memoize rails ruby ruby-on-rails
Last synced: 10 days ago
JSON representation
This gem is an extension to the standard memoization pattern.
- Host: GitHub
- URL: https://github.com/freshworks/memoize_until
- Owner: freshworks
- License: mit
- Created: 2017-10-23T07:54:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-30T04:36:02.000Z (14 days ago)
- Last Synced: 2024-10-30T20:48:50.871Z (13 days ago)
- Topics: cache, memoize, rails, ruby, ruby-on-rails
- Language: Ruby
- Homepage:
- Size: 48.8 KB
- Stars: 1
- Watchers: 9
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# MemoizeUntil
This gem is an extension to the standard `memoization` pattern for storing expensive computations or network calls `in-memory`. Unlike other memoization extensions which expire after a pre-defined interval, this gem provides a consistent memoization behavior across multiple processes/servers i.e. keys expire simultaneously across all processes.
Usage:
```ruby
gem install memoize_until
> irb
irb:> require 'memoize_until'
irb:> result = MemoizeUntil.day(:default) do
irb:> # PerformSomeComplexOperation
irb:> end # memoizes(until the end of the day) and returns the result of #PerformSomeComplexOperation
irb:> p result.inspect
```The default API that the gem provides is: `MemoizeUntil#min, MemoizeUntil#hour, MemoizeUntil#day, MemoizeUntil#week, MemoizeUntil#month` with `default` purposes(keys).
To add new purposes during run_time, you can also leverage the `add_to` API:
```ruby
irb:> MemoizeUntil.add_to(:day, :runtime_key)
irb:> result = MemoizeUntil.day(:runtime_key) do
irb:> # PerformSomeComplexRuntimeOperation
irb:> end # memoizes(until the end of the day) and returns the result of #PerformSomeComplexOperation
irb:> p result.inspect
```
The same can be done for other default kinds as well: `min, hour, week, month`## Rails
To use this gem in a rails application, add the following line to your `Gemfile` and you are good to go.
```ruby
gem 'memoize_until'
```For most use cases, the list of purposes that come will not suffice. You can define your custom list of config purposes that you wish to memoize for, by including a `config/memoize_until.yml` in the root directory of your application. Here is an [example](/examples/memoize_until.yml) to help you with the file structure.
## Testing
To clear the currently memoized value for a purpose, you can use the `clear_now` API.
```ruby
irb:> MemoizeUntil.day(:default) { 1 } # 1
irb:> MemoizeUntil.clear_now_for(:day, :default)
irb:> MemoizeUntil.day(:default) { 2 } # 2
```#### Note: This API only clears the currently memoized value in the current running process and will not mitigate to other processes in a multiprocess world. This is recommended to be used only for testing setup.
## Contributing
To run test cases,
```shell
bundle install
ruby -Ilib:test test/memoize_until_test.rb
```This project is Licensed under the MIT License. Further details can be found [here](/LICENSE).