smart_cache_store 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: 5afc7011d21ae7f81c857be6283535b4d036eb126a2a890412c1f98fca5f914a
4
+ data.tar.gz: cdc421218f4a6963adbd0f7074c83cfa277d3986f29915482b45dc5ff911e8c0
5
+ SHA512:
6
+ metadata.gz: 70b98d97da2015b7fd4512cf40fdf89aa0b1f3de7b70193cc7db315aaca6055d2754c4dbcd0c465018f9db3866faa48387c76d47eecab9914128f956c2bad5a1
7
+ data.tar.gz: '08e1b0c805e7343bf4b07cadbb0703ae7d1383888175246b526f89c85f2cda95f707fae42b7c36712c7d96b4eb7c68bae1d78afb5f0adc8597a6588bb9c4bfd7'
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 thomas morgan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # SmartCacheStore
2
+
3
+ SmartCacheStore provides variations on ActiveSupport's MemCacheStore and RedisCacheStore to make the default configurations more production ready. It does this by setting better defaults and using a few more ENVs.
4
+
5
+
6
+ ##### Redis -- SmartRedisCache
7
+
8
+ Set by default:
9
+
10
+ url: ENV['REDIS_URL']
11
+ namespace: ENV['REDIS_NAMESPACE']
12
+ expires_in: 1.day
13
+ race_condition_ttl: 5.seconds
14
+ pool_size: ENV['RAILS_MAX_THREADS'] || 5
15
+ connect_timeout: 2
16
+ read_timeout: 1
17
+ write_timeout: 1
18
+ reconnect_attempts: 1
19
+ reconnect_delay: 0.2
20
+
21
+ `:url` should be a uri, like: `redis://localhost:6379/0`. For two or more uris, combine with a comma: `redis://host-a,redis://host-b`.
22
+
23
+ Any of these may still be specified directly, as well as all other support parameters for RedisCacheStore or the Redis gem.
24
+
25
+
26
+ ##### Memcache -- SmartMemCache
27
+
28
+ Set by default:
29
+
30
+ url: ENV['MEMCACHE_SERVERS']
31
+ namespace: ENV['MEMCACHE_NAMESPACE']
32
+ expires_in: 1.day
33
+ race_condition_ttl: 5.seconds
34
+ failover: true
35
+ pool_size: ENV['RAILS_MAX_THREADS'] || 5
36
+
37
+ MemCacheStore's legacy initializer with `*addresses` as the first parameter, and the RedisCacheStore compatible `:url` are supported.
38
+
39
+ `:url` or *addresses should be a uri, like: `memcached://localhost:11211`. For two or more uris, combine with a comma: `memcached://host-a,memcached://host-b`.
40
+
41
+
42
+ ### Other features
43
+
44
+ SmartMemCache's increment() and decrement() are also enhanced to be able to act on previously unset keys.
45
+
46
+
47
+ ## Usage
48
+
49
+ In `environment/[env].rb`:
50
+
51
+ #### Redis
52
+
53
+ # basic:
54
+ config.cache_store = :smart_redis_cache
55
+ # with parameters:
56
+ config.cache_store = :smart_redis_cache, {url: 'redis://localhost:1234/9'}
57
+
58
+ #### Memcache
59
+
60
+ # basic:
61
+ config.cache_store = :smart_mem_cache
62
+ # with parameters:
63
+ config.cache_store = :smart_mem_cache, 'memcached://localhost:1234'
64
+ config.cache_store = :smart_mem_cache, 'memcached://localhost:1234', {expires_in: 1.hour}
65
+ config.cache_store = :smart_mem_cache, {url: 'memcached://localhost:1234', expires_in: 1.hour}
66
+
67
+
68
+
69
+ ## Installation
70
+ Add this line to your application's Gemfile:
71
+
72
+ ```ruby
73
+ gem "smart_cache_store"
74
+ ```
75
+
76
+ And then execute:
77
+ ```bash
78
+ $ bundle
79
+ ```
80
+
81
+
82
+ ## Contributing
83
+
84
+ 1. Fork it ( https://github.com/zarqman/smart_cache_store/fork )
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Push to the branch (`git push origin my-new-feature`)
88
+ 5. Create new Pull Request
89
+
90
+ ## License
91
+ 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,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.pattern = "test/**/*_test.rb"
7
+ t.verbose = false
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,63 @@
1
+ # requires gems: dalli, connection_pool
2
+ #
3
+ # MEMCACHE_SERVERS can be either
4
+ # server1:11211,server2
5
+ # or
6
+ # memcached://user:pass@server1:11211,...
7
+ # default: localhost:11211
8
+
9
+
10
+ require 'active_support/cache/mem_cache_store'
11
+
12
+ module ActiveSupport
13
+ module Cache
14
+ class SmartMemCache < MemCacheStore
15
+
16
+ # known options:
17
+ # ActiveSupport::Cache - universal options
18
+ # :namespace{nil}, :compress{true}, :compress_threshold{1k}, :expires_in{0}, :race_condition_ttl{0}
19
+ # ActiveSupport::Cache::Store
20
+ # :pool_size{5}, :pool_timeout{5}
21
+ # Dalli::Client
22
+ # :failover{true}, :serializer{Marshall}, :compressor{zlib}, :cache_nils{false}
23
+ # :namespace{nil}, :expires_in{0}, :compress{false} (separate from AS::C's same options above)
24
+ # :threadsafe{true} (using ConnPool turns this off automatically)
25
+ def initialize(*addresses, **args)
26
+ args.reverse_merge!(
27
+ namespace: ENV['MEMCACHE_NAMESPACE'],
28
+ expires_in: 1.day,
29
+ race_condition_ttl: 5.seconds,
30
+ failover: true,
31
+ pool_size: ENV.fetch('RAILS_MAX_THREADS'){ 5 }.to_i,
32
+ )
33
+ addresses.push Array(args.delete(:url)) if addresses.empty? && args.key?(:url)
34
+ super(*addresses, args)
35
+ end
36
+
37
+
38
+ # MemCacheStore#increment docs say it will init any invalid value to 0.
39
+ # In reality, it will only increment a pre-existing, raw value. everything else returns nil or an error.
40
+ # This fixes init of missing value on both increment() and decrement().
41
+ # Preexisting, invalid values still return errors.
42
+
43
+ def increment(name, amount = 1, options = nil)
44
+ options = merged_options(options)
45
+ instrument(:increment, name, amount: amount) do
46
+ rescue_error_with nil do
47
+ @data.with { |c| c.incr(normalize_key(name, options), amount, options[:expires_in], amount) }
48
+ end
49
+ end
50
+ end
51
+
52
+ def decrement(name, amount = 1, options = nil)
53
+ options = merged_options(options)
54
+ instrument(:decrement, name, amount: amount) do
55
+ rescue_error_with nil do
56
+ @data.with { |c| c.decr(normalize_key(name, options), amount, options[:expires_in], 0) }
57
+ end
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,45 @@
1
+ # requires gems: redis, connection_pool
2
+ #
3
+ # REDIS_URL (or :url) can have one or multiple urls:
4
+ # redis://localhost:1234
5
+ # redis://host-a,redis://host-b # automatically uses Redis::Distributed
6
+
7
+
8
+ require 'active_support/cache/redis_cache_store'
9
+
10
+ module ActiveSupport
11
+ module Cache
12
+ class SmartRedisCache < RedisCacheStore
13
+
14
+ # known options:
15
+ # ActiveSupport::Cache - universal options
16
+ # :namespace{nil}, :compress{true}, :compress_threshold{1k}, :expires_in{0}, :race_condition_ttl{0}
17
+ # ActiveSupport::Cache::Store
18
+ # :pool_size{5}, :pool_timeout{5}
19
+ # Redis
20
+ # :host, :port, :db, :username, :password, :url, :path
21
+ # ssl_params: {:ca_file, :cert, :key}
22
+ # :sentinels, :role
23
+ # :cluster, :replica
24
+ # :timeout (sets next 3), :connect_timeout, :read_timeout, :write_timeout
25
+ # :reconnect_attempts, :reconnect_delay, :reconnect_delay_max
26
+ def initialize(**args)
27
+ args.reverse_merge!(
28
+ namespace: ENV['REDIS_NAMESPACE'],
29
+ expires_in: 1.day,
30
+ race_condition_ttl: 5.seconds,
31
+ pool_size: ENV.fetch('RAILS_MAX_THREADS'){ 5 }.to_i,
32
+ connect_timeout: 2,
33
+ read_timeout: 1,
34
+ write_timeout: 1,
35
+ reconnect_attempts: 1,
36
+ reconnect_delay: 0.2,
37
+ )
38
+ args[:url] ||= ENV['REDIS_URL'] unless args.key?(:redis) || args.key?(:url)
39
+ args[:url] = args[:url].split(',') if args[:url].is_a?(String)
40
+ super(**args)
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module SmartCacheStore
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1 @@
1
+ require 'smart_cache_store/version'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :smart_cache_store do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_cache_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - thomas morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-18 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: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ description:
28
+ email:
29
+ - tm@iprog.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - Rakefile
37
+ - lib/active_support/cache/smart_mem_cache.rb
38
+ - lib/active_support/cache/smart_redis_cache.rb
39
+ - lib/smart_cache_store.rb
40
+ - lib/smart_cache_store/version.rb
41
+ - lib/tasks/smart_cache_store_tasks.rake
42
+ homepage: https://github.com/zarqman/smart_cache_store
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ homepage_uri: https://github.com/zarqman/smart_cache_store
47
+ changelog_uri: https://github.com/zarqman/smart_cache_store/blob/master/CHANGELOG.md
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.2.22
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Production-ready enhancements for mem_cache_store and redis_cache_store
67
+ test_files: []