leveled_cache 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a86654509725147ae338c2043e6d7f8ba9d5df587b2c34cf93a65000c30d10d
4
+ data.tar.gz: ca9185afc78fe514d66abb0264cb45031c88c512f9cfec7362dcb65bdcf2881c
5
+ SHA512:
6
+ metadata.gz: 4a316bcdfee2317c1b63b52660e169bfa5b115b3aed273cb4dea60051f81ff47ce417bf33c44481e6f496bafdfef35a9b5ff406ca8f07c685baa0be8d32dd749
7
+ data.tar.gz: 16ef1640ba78fc68f7823bea32bc8278654fa6e4ac5f9af36aba079abc3ab73a99c58e10f471cbcc6c0fea03743b435ec749c46adb8df0d8551a0d5424139546
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,50 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ TargetRubyVersion: 3.1
8
+
9
+ Bundler/OrderedGems:
10
+ Exclude:
11
+ - gemfiles/*
12
+
13
+ Metrics/BlockLength:
14
+ Max: 200
15
+
16
+ Metrics/MethodLength:
17
+ Max: 16
18
+
19
+ Style/ArgumentsForwarding:
20
+ UseAnonymousForwarding: false
21
+
22
+ Style/FrozenStringLiteralComment:
23
+ Exclude:
24
+ - gemfiles/*
25
+
26
+ Style/StringLiterals:
27
+ Enabled: true
28
+ EnforcedStyle: double_quotes
29
+
30
+ Style/StringLiteralsInInterpolation:
31
+ Enabled: true
32
+ EnforcedStyle: double_quotes
33
+
34
+ Layout/LineLength:
35
+ Max: 120
36
+
37
+ Lint/MissingSuper:
38
+ Enabled: false
39
+
40
+ RSpec/ExampleLength:
41
+ Max: 7
42
+
43
+ RSpec/MessageSpies:
44
+ EnforcedStyle: receive
45
+
46
+ RSpec/MultipleExpectations:
47
+ Max: 5
48
+
49
+ RSpec/MultipleMemoizedHelpers:
50
+ Max: 6
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ %w[6.1 7.0 7.1].each do |version|
4
+ appraise "activesupport-#{version}" do
5
+ gem "activesupport", "~> #{version}.0"
6
+ end
7
+ end
8
+
9
+ appraise "activesupport-main" do
10
+ gem "activesupport", git: "https://github.com/rails/rails", glob: "activesupport/*.gemspec"
11
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-02-13
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at dpirotte@paypal.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "appraisal"
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 1"
11
+ gem "rubocop-rake", "~> 0"
12
+ gem "rubocop-rspec", "~> 2.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Dave Pirotte
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # LeveledCache
2
+
3
+ An `ActiveSupport::Cache::Store` implementation that acts as a proxy over multiple caches ("levels") that are called in order. Caches specified earlier are checked for values earlier.
4
+
5
+ A typical use-case is to put a smaller, faster `ActiveSupport::Cache::MemoryStore` in front of a larger, slower `ActiveSupport::Cache::RedisCacheStore` to keep the hottest keys populated close to application code.
6
+
7
+ All interactions use the underlying caches' public APIs, which means that an interaction on the LeveledProxy potentially serializes and compresses the data multiple times as the interaction progresses through levels. Take care to configure underlying caches accordingly. For example, it may be desirable to use `NullCoder` on an outer `ActiveSupport::Cache::MemoryStore`.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add leveled_cache
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install leveled_cache
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ FastCache = ActiveSupport::Cache::MemoryStore.new # fast, in-process cache
23
+ SlowCache = ActiveSupport::Cache::RedisCacheStore.new # slow, out-of-process cache
24
+
25
+ cache = LeveledCache::Store.new(FastCache, SlowCache)
26
+
27
+ cache.fetch("key") do
28
+ # 1. Fetch from FastCache
29
+ # 2. If missing, fetch from SlowCache and populate FastCache
30
+ # 3. If missing, run block, populate SlowCache and FastCache
31
+ compute_expensive_value
32
+ end
33
+
34
+ cache.fetch_multi("key1", "key2") do |key|
35
+ # 1. fetch_multi from FastCache
36
+ # 2. For any missing values, fetch_multi from SlowCache and populate FastCache
37
+ # 3. For any missing values, run block, populate SlowCache and FastCache
38
+ compute_expensive_value_for(key)
39
+ end
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dpirotte/leveled_cache. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/activesupport-cache-leveled_proxy/blob/main/CODE_OF_CONDUCT.md).
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
55
+
56
+ ## Code of Conduct
57
+
58
+ Everyone interacting in the LeveledCache project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/activesupport-cache-leveled_proxy/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rake", "~> 13.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "rubocop", "~> 1"
9
+ gem "rubocop-rake", "~> 0"
10
+ gem "rubocop-rspec", "~> 2.0"
11
+ gem "activesupport", "~> 6.1.0"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rake", "~> 13.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "rubocop", "~> 1"
9
+ gem "rubocop-rake", "~> 0"
10
+ gem "rubocop-rspec", "~> 2.0"
11
+ gem "activesupport", "~> 7.0.0"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rake", "~> 13.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "rubocop", "~> 1"
9
+ gem "rubocop-rake", "~> 0"
10
+ gem "rubocop-rspec", "~> 2.0"
11
+ gem "activesupport", "~> 7.1.0"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "rake", "~> 13.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "rubocop", "~> 1"
9
+ gem "rubocop-rake", "~> 0"
10
+ gem "rubocop-rspec", "~> 2.0"
11
+ gem "activesupport", git: "https://github.com/rails/rails", glob: "activesupport/*.gemspec"
12
+
13
+ gemspec path: "../"
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/cache"
4
+ require "active_support/notifications"
5
+
6
+ module LeveledCache
7
+ # A cache store implementation that acts as a proxy over multiple caches
8
+ # ("levels") that are called in order. Caches specified earlier are
9
+ # checked for values earlier.
10
+ #
11
+ # A typical use-case is to put a fast, in-process MemoryStore in front of
12
+ # a larger, slower RedisCacheStore:
13
+ #
14
+ # cache = LeveledCache::Store.new(
15
+ # ActiveSupport::Cache::MemoryStore.new,
16
+ # ActiveSupport::Cache::RedisCacheStore.new,
17
+ # )
18
+ #
19
+ # All interactions use the underlying caches' public APIs, which means
20
+ # an interaction on the LeveledProxy potentially serialized, compressed,
21
+ # etc. multiple times as the interaction progresses through levels. Take
22
+ # care to configure underlying caches accordingly. For example, it may
23
+ # be desirable to use NullCoder on an earlier MemoryStore.
24
+ class Store < ActiveSupport::Cache::Store
25
+ VERSION = "0.1.0"
26
+
27
+ # Create a new LeveledCacheProxy that calls cache methods on
28
+ # one or more underlying caches in order.
29
+ #
30
+ # ==== Options
31
+ #
32
+ # No options are allowed. Set options on the caches provided.
33
+ #
34
+ def initialize(*caches)
35
+ @caches = caches
36
+ @options = {}
37
+ end
38
+
39
+ # Fetches data from the caches in order using the given key. Data
40
+ # is returned from the first cache with data for the given key.
41
+ #
42
+ # Internally, +fetch+ is called on each cache level recursively
43
+ # until data is found. If data is missing from "earlier" caches but
44
+ # found in "later" caches, all "earlier" caches will populate with
45
+ # the found data.
46
+ #
47
+ # If no data is found in any caches, the block is called and all
48
+ # caches are populated with the return value.
49
+ #
50
+ # Options are passed through to the underlying caches.
51
+ def fetch(name, **options, &)
52
+ _fetch(@caches, name, options, &)
53
+ end
54
+
55
+ # Fetches data from the caches in order using the given keys. Data
56
+ # is returned from the first cache with data for the given keys.
57
+ #
58
+ # If no data is found in any caches, the block is called and all
59
+ # caches are populated with the return value.
60
+ #
61
+ # Options are passed through to the underlying caches.
62
+ def fetch_multi(*names, **options, &)
63
+ _fetch_multi(@caches, names, options, &)
64
+ end
65
+
66
+ # Reads data from the caches in order using the given key. Data
67
+ # is returned from the first cache level with data for the given
68
+ # key. Otherwise, +nil+ is returned.
69
+ #
70
+ # Options are passed through to the underlying caches.
71
+ def read(name, **options)
72
+ read_entry(name, **options)
73
+ end
74
+
75
+ # Writes the value to all cache levels with the provided key.
76
+ #
77
+ # Options are passed through to the underlying caches.
78
+ def write(name, value, **options)
79
+ write_entry(name, value, **options)
80
+ end
81
+
82
+ private
83
+
84
+ # Recursively `fetch_multi` on each cache. Missing values
85
+ # will attempt to `fetch_multi` from the next cache level
86
+ # until all values are returned, or the block is called.
87
+ def _fetch_multi(caches, names, options, &)
88
+ first, *rest = caches
89
+
90
+ reads = first.read_multi(*names, **options)
91
+ missing = names - reads.keys
92
+
93
+ if missing.any?
94
+ writes = if rest.any?
95
+ _fetch_multi(rest, missing, options, &)
96
+ else
97
+ missing.index_with(&)
98
+ end
99
+
100
+ first.write_multi(writes, options)
101
+ reads.merge!(writes)
102
+ end
103
+
104
+ reads
105
+ end
106
+
107
+ # Recursively `fetch` on each cache level until a value
108
+ # is returned or the block is called.
109
+ def _fetch(caches, name, options, &block)
110
+ first, *rest = caches
111
+
112
+ first.fetch(name, options) do
113
+ if rest.any?
114
+ _fetch(rest, name, options, &block)
115
+ else
116
+ block.call
117
+ end
118
+ end
119
+ end
120
+
121
+ def read_entry(key, **options)
122
+ @caches.lazy.map do |cache|
123
+ cache.read(key, options)
124
+ end.compact.first
125
+ end
126
+
127
+ def write_entry(key, entry, **options)
128
+ @caches.each do |cache|
129
+ cache.write(key, entry, options)
130
+ end
131
+ end
132
+
133
+ def delete_entry(key, **options)
134
+ @caches.each do |cache|
135
+ cache.delete(key, options)
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LeveledCache
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "leveled_cache/store"
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leveled_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dave Pirotte
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '6.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.2'
33
+ description:
34
+ email:
35
+ - dpirotte@gmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - ".rspec"
41
+ - ".rubocop.yml"
42
+ - Appraisals
43
+ - CHANGELOG.md
44
+ - CODE_OF_CONDUCT.md
45
+ - Gemfile
46
+ - LICENSE.txt
47
+ - README.md
48
+ - Rakefile
49
+ - gemfiles/activesupport_6.1.gemfile
50
+ - gemfiles/activesupport_7.0.gemfile
51
+ - gemfiles/activesupport_7.1.gemfile
52
+ - gemfiles/activesupport_main.gemfile
53
+ - lib/leveled_cache.rb
54
+ - lib/leveled_cache/store.rb
55
+ - lib/leveled_cache/version.rb
56
+ homepage: https://github.com/dpirotte/leveled_cache
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/dpirotte/leveled_cache
61
+ rubygems_mfa_required: 'true'
62
+ source_code_uri: https://github.com/dpirotte/leveled_cache
63
+ changelog_uri: https://github.com/dpirotte/leveled_cache/blob/main/CHANGELOG.md
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '3.1'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.4.1
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: ActiveSupport::Cache that wraps multiple levels of caches
83
+ test_files: []