rails_simple_caching 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 03a870f37e5d45c8fe45c58720d121b6daf88c68f4e653c85bfb625a97b57e3c
4
+ data.tar.gz: 1868335440ee6a526daefea845c2922f8361f81a38c17edd1b8bbfd99575eb23
5
+ SHA512:
6
+ metadata.gz: 282c5968348b213714ca0ef293a571affca13ba8de9252aa3a4654cd0aafee3e8524656ded8257aeecb5b6542b1d80e572e772ec1fe3ddfe73d647f324fd02b9
7
+ data.tar.gz: ba4c6d56d5f1ad5245a9b189af731b2e87d478fafb8cb54951ece53d6eedccf589283e789384cccb4f6bb86af9cb127f3c2c238849bd631d07edd93e761f0718
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ /spec/dummy/storage/*
11
+ /spec/dummy/log/*
12
+ /spec/dummy/tmp/*
13
+ /spec/dummy/test/*
14
+ spec/dummy/db/development.sqlite3
15
+ !/spec/dummy/tmp/caching-dev.txt
16
+
17
+ # rspec failure tracking
18
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Layout/LineLength:
2
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rails_simple_caching.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rails", "~> 6.1"
11
+ gem 'sqlite3', '~> 1.4', group: :development
12
+
13
+ gem "rspec", "~> 3.10"
14
+
15
+ gem "rubocop", "~> 1.22"
data/Gemfile.lock ADDED
@@ -0,0 +1,190 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_simple_caching (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (6.1.4.1)
10
+ actionpack (= 6.1.4.1)
11
+ activesupport (= 6.1.4.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (6.1.4.1)
15
+ actionpack (= 6.1.4.1)
16
+ activejob (= 6.1.4.1)
17
+ activerecord (= 6.1.4.1)
18
+ activestorage (= 6.1.4.1)
19
+ activesupport (= 6.1.4.1)
20
+ mail (>= 2.7.1)
21
+ actionmailer (6.1.4.1)
22
+ actionpack (= 6.1.4.1)
23
+ actionview (= 6.1.4.1)
24
+ activejob (= 6.1.4.1)
25
+ activesupport (= 6.1.4.1)
26
+ mail (~> 2.5, >= 2.5.4)
27
+ rails-dom-testing (~> 2.0)
28
+ actionpack (6.1.4.1)
29
+ actionview (= 6.1.4.1)
30
+ activesupport (= 6.1.4.1)
31
+ rack (~> 2.0, >= 2.0.9)
32
+ rack-test (>= 0.6.3)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
35
+ actiontext (6.1.4.1)
36
+ actionpack (= 6.1.4.1)
37
+ activerecord (= 6.1.4.1)
38
+ activestorage (= 6.1.4.1)
39
+ activesupport (= 6.1.4.1)
40
+ nokogiri (>= 1.8.5)
41
+ actionview (6.1.4.1)
42
+ activesupport (= 6.1.4.1)
43
+ builder (~> 3.1)
44
+ erubi (~> 1.4)
45
+ rails-dom-testing (~> 2.0)
46
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
47
+ activejob (6.1.4.1)
48
+ activesupport (= 6.1.4.1)
49
+ globalid (>= 0.3.6)
50
+ activemodel (6.1.4.1)
51
+ activesupport (= 6.1.4.1)
52
+ activerecord (6.1.4.1)
53
+ activemodel (= 6.1.4.1)
54
+ activesupport (= 6.1.4.1)
55
+ activestorage (6.1.4.1)
56
+ actionpack (= 6.1.4.1)
57
+ activejob (= 6.1.4.1)
58
+ activerecord (= 6.1.4.1)
59
+ activesupport (= 6.1.4.1)
60
+ marcel (~> 1.0.0)
61
+ mini_mime (>= 1.1.0)
62
+ activesupport (6.1.4.1)
63
+ concurrent-ruby (~> 1.0, >= 1.0.2)
64
+ i18n (>= 1.6, < 2)
65
+ minitest (>= 5.1)
66
+ tzinfo (~> 2.0)
67
+ zeitwerk (~> 2.3)
68
+ ast (2.4.2)
69
+ builder (3.2.4)
70
+ concurrent-ruby (1.1.9)
71
+ crass (1.0.6)
72
+ diff-lcs (1.4.4)
73
+ erubi (1.10.0)
74
+ globalid (1.0.0)
75
+ activesupport (>= 5.0)
76
+ i18n (1.8.11)
77
+ concurrent-ruby (~> 1.0)
78
+ loofah (2.12.0)
79
+ crass (~> 1.0.2)
80
+ nokogiri (>= 1.5.9)
81
+ mail (2.7.1)
82
+ mini_mime (>= 0.1.1)
83
+ marcel (1.0.2)
84
+ method_source (1.0.0)
85
+ mini_mime (1.1.2)
86
+ minitest (5.14.4)
87
+ nio4r (2.5.8)
88
+ nokogiri (1.12.5-x86_64-linux)
89
+ racc (~> 1.4)
90
+ parallel (1.21.0)
91
+ parser (3.0.2.0)
92
+ ast (~> 2.4.1)
93
+ racc (1.6.0)
94
+ rack (2.2.3)
95
+ rack-test (1.1.0)
96
+ rack (>= 1.0, < 3)
97
+ rails (6.1.4.1)
98
+ actioncable (= 6.1.4.1)
99
+ actionmailbox (= 6.1.4.1)
100
+ actionmailer (= 6.1.4.1)
101
+ actionpack (= 6.1.4.1)
102
+ actiontext (= 6.1.4.1)
103
+ actionview (= 6.1.4.1)
104
+ activejob (= 6.1.4.1)
105
+ activemodel (= 6.1.4.1)
106
+ activerecord (= 6.1.4.1)
107
+ activestorage (= 6.1.4.1)
108
+ activesupport (= 6.1.4.1)
109
+ bundler (>= 1.15.0)
110
+ railties (= 6.1.4.1)
111
+ sprockets-rails (>= 2.0.0)
112
+ rails-dom-testing (2.0.3)
113
+ activesupport (>= 4.2.0)
114
+ nokogiri (>= 1.6)
115
+ rails-html-sanitizer (1.4.2)
116
+ loofah (~> 2.3)
117
+ railties (6.1.4.1)
118
+ actionpack (= 6.1.4.1)
119
+ activesupport (= 6.1.4.1)
120
+ method_source
121
+ rake (>= 0.13)
122
+ thor (~> 1.0)
123
+ rainbow (3.0.0)
124
+ rake (13.0.6)
125
+ regexp_parser (2.2.0)
126
+ rexml (3.2.5)
127
+ rspec (3.10.0)
128
+ rspec-core (~> 3.10.0)
129
+ rspec-expectations (~> 3.10.0)
130
+ rspec-mocks (~> 3.10.0)
131
+ rspec-core (3.10.1)
132
+ rspec-support (~> 3.10.0)
133
+ rspec-expectations (3.10.1)
134
+ diff-lcs (>= 1.2.0, < 2.0)
135
+ rspec-support (~> 3.10.0)
136
+ rspec-mocks (3.10.2)
137
+ diff-lcs (>= 1.2.0, < 2.0)
138
+ rspec-support (~> 3.10.0)
139
+ rspec-rails (5.0.2)
140
+ actionpack (>= 5.2)
141
+ activesupport (>= 5.2)
142
+ railties (>= 5.2)
143
+ rspec-core (~> 3.10)
144
+ rspec-expectations (~> 3.10)
145
+ rspec-mocks (~> 3.10)
146
+ rspec-support (~> 3.10)
147
+ rspec-support (3.10.2)
148
+ rubocop (1.22.3)
149
+ parallel (~> 1.10)
150
+ parser (>= 3.0.0.0)
151
+ rainbow (>= 2.2.2, < 4.0)
152
+ regexp_parser (>= 1.8, < 3.0)
153
+ rexml
154
+ rubocop-ast (>= 1.12.0, < 2.0)
155
+ ruby-progressbar (~> 1.7)
156
+ unicode-display_width (>= 1.4.0, < 3.0)
157
+ rubocop-ast (1.13.0)
158
+ parser (>= 3.0.1.1)
159
+ ruby-progressbar (1.11.0)
160
+ sprockets (4.0.2)
161
+ concurrent-ruby (~> 1.0)
162
+ rack (> 1, < 3)
163
+ sprockets-rails (3.4.1)
164
+ actionpack (>= 5.2)
165
+ activesupport (>= 5.2)
166
+ sprockets (>= 3.0.0)
167
+ sqlite3 (1.4.2)
168
+ thor (1.1.0)
169
+ tzinfo (2.0.4)
170
+ concurrent-ruby (~> 1.0)
171
+ unicode-display_width (2.1.0)
172
+ websocket-driver (0.7.5)
173
+ websocket-extensions (>= 0.1.0)
174
+ websocket-extensions (0.1.5)
175
+ zeitwerk (2.5.1)
176
+
177
+ PLATFORMS
178
+ x86_64-linux
179
+
180
+ DEPENDENCIES
181
+ rails (~> 6.1)
182
+ rails_simple_caching!
183
+ rake (~> 13.0)
184
+ rspec (~> 3.10)
185
+ rspec-rails
186
+ rubocop (~> 1.22)
187
+ sqlite3 (~> 1.4)
188
+
189
+ BUNDLED WITH
190
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 bulgakke
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,104 @@
1
+ # RailsSimpleCaching
2
+
3
+ A simple wrapper around Rails built-in caching. Provides Rails-like macros to generate `cached_` versions of your methods. The original goal was to reduce DB queries, but it works with any methods you define (as long as they don't take any arguments, though it's doable to make it work for methods with arguments, too).
4
+ How Rails caching works: https://guides.rubyonrails.org/caching_with_rails.html#low-level-caching
5
+ https://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html
6
+
7
+
8
+ https://github.com/igorkasyanchuk/rails_cached_method is the only gem of a similar caliber that does kind of the same thing. That one's functionality is wider, but it does a little bit too much for my taste.
9
+
10
+ ## Example of usage:
11
+
12
+ ```ruby
13
+ class Author < ApplicationRecord
14
+ has_many :books
15
+
16
+ cache_expires_in 96.hours
17
+ caches :books
18
+ caches :expensive_method, expires_in: 2.hours
19
+
20
+ def expensive_method
21
+ SomeExternalAPIService.call(self)
22
+ do_something_else
23
+ end
24
+ end
25
+ ```
26
+
27
+ The above is equivalent to:
28
+ ```ruby
29
+ class Author < ApplicationRecord
30
+ has_many :books
31
+
32
+ def expensive_method
33
+ SomeExternalAPIService.call(self)
34
+ do_something_else
35
+ end
36
+
37
+ def cached_books
38
+ Rails.cache.fetch("#{cache_key_with_version}/books", expires_in: 96.hours) do
39
+ books
40
+ end
41
+ end
42
+
43
+ def cached_expensive_method
44
+ Rails.cache.fetch("#{cache_key_with_version}/expensive_method", expires_in: 2.hours) do
45
+ expensive_method
46
+ end
47
+ end
48
+ end
49
+ ```
50
+
51
+ Rails caching won't work in development by default; run `rails dev:cache` in terminal.
52
+
53
+ ## Cache expiration time
54
+ Use these methods for setting up expiration time (how long until the data is deleted from the cache store). All of them expect a `ActiveSupport::Duration` argument (`24.hours`, `10.minutes` etc.)
55
+ The precedence goes:
56
+
57
+ 1. `:expires_in` keyword argument.
58
+ 2. `cache_expires_in` class method. Sets default expiration time for all `caches` macros in the model class. Will only work on macros that are declared below it, for example:
59
+
60
+ ```ruby
61
+ caches :something # Cache will still expire after default time
62
+ cache_expires_in 2.minutes
63
+ caches :something_else # Will expire in 2 minutes
64
+ cache_expires_in 3.hours
65
+ caches :another_something # Will expire in 3 hours
66
+ ```
67
+ 3. `Rails.configuration.default_expire_time`: global default for all models. Set to `24.hours` by default. Set it to your liking in `config/application.rb` or in `config/environments/`
68
+
69
+ ## Cache invalidation
70
+ The cache is invalidated whenever an object with a `cached_` method has its `updated_at` value changed (because [this method](https://api.rubyonrails.org/classes/ActiveRecord/Integration.html#method-i-cache_key_with_version) is used to form the key by which the data is fetched). The data is not deleted from the store, it just won't be fetched the next time you call the method.
71
+
72
+ `# TODO: write some more about cache invalidation`
73
+
74
+
75
+ ## Installation
76
+
77
+ Add this line to your application's Gemfile:
78
+
79
+ ```ruby
80
+ gem 'rails_simple_caching'
81
+ ```
82
+
83
+ And then execute:
84
+
85
+ $ bundle install
86
+
87
+ Or install it yourself as:
88
+
89
+ $ gem install rails_simple_caching
90
+
91
+
92
+ ## Development
93
+
94
+ 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.
95
+
96
+ 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).
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_simple_caching.
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsSimpleCaching
4
+ module Caching
5
+ module ClassMethods
6
+ def cache_expires_in(time)
7
+ @expire_time = time
8
+ end
9
+
10
+ def caches(attribute, expires_in: expire_time)
11
+ @cached_attributes ||= []
12
+ @cached_attributes << attribute
13
+
14
+ # `cache_key_with_version` generates a string based on the
15
+ # model's class name, `id`, and `updated_at` attributes.
16
+ # This is a common convention and has the benefit of invalidating
17
+ # the cache whenever the product is updated.
18
+ define_method("cached_#{attribute}") do
19
+ key = "#{cache_key_with_version}/#{attribute}"
20
+ Rails.cache.fetch(key, expires_in: expires_in) do
21
+ update_cached(attribute)
22
+ end
23
+ end
24
+ end
25
+
26
+ def cached_attributes
27
+ @cached_attributes
28
+ end
29
+
30
+ private
31
+
32
+ def expire_time
33
+ @expire_time || Rails.configuration.default_expire_time
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsSimpleCaching
4
+ module Caching
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ extend ClassMethods
9
+ end
10
+
11
+ def update_cached(attribute)
12
+ contents = public_send(attribute)
13
+ key = "#{cache_key_with_version}/#{attribute}"
14
+ success = Rails.cache.write(key, contents)
15
+ success ? contents : false
16
+ end
17
+
18
+ # Updates all the cached attributes.
19
+ # Returns true if every update is successful.
20
+ def update_cache
21
+ self.class.cached_attributes.map { |attr| update_cached(attr) }.none?(false)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module RailsSimpleCaching
2
+ class Railtie < ::Rails::Railtie
3
+ config.default_expire_time = 24.hours
4
+
5
+ initializer 'rails_simple_caching.include_caching_concern' do
6
+ ActiveSupport.on_load :active_record do
7
+ ActiveRecord::Base.include Caching
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsSimpleCaching
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rails_simple_caching/version"
4
+ require_relative "rails_simple_caching/railtie" if defined?(Rails::Railtie)
5
+ require_relative "rails_simple_caching/caching"
6
+ require_relative "rails_simple_caching/caching/class_methods"
7
+
8
+ module RailsSimpleCaching
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rails_simple_caching/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rails_simple_caching"
7
+ spec.version = RailsSimpleCaching::VERSION
8
+ spec.authors = ["bulgakke"]
9
+ spec.email = ["vvp835@yandex.ru"]
10
+
11
+ spec.summary = "A simple macro-like interface for Rails caching"
12
+ # spec.description = "TODO: Write a longer description or delete this line."
13
+ spec.homepage = "https://github.com/bulgakke/rails_simple_caching"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/bulgakke/rails_simple_caching"
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency 'rspec-rails'
33
+
34
+ # For more information and examples about making a new gem, checkout our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_simple_caching
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - bulgakke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - vvp835@yandex.ru
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/rails_simple_caching.rb
43
+ - lib/rails_simple_caching/caching.rb
44
+ - lib/rails_simple_caching/caching/class_methods.rb
45
+ - lib/rails_simple_caching/railtie.rb
46
+ - lib/rails_simple_caching/version.rb
47
+ - rails_simple_caching.gemspec
48
+ homepage: https://github.com/bulgakke/rails_simple_caching
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/bulgakke/rails_simple_caching
53
+ source_code_uri: https://github.com/bulgakke/rails_simple_caching
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.3.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.6
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A simple macro-like interface for Rails caching
73
+ test_files: []