kredis 0.3.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kredis might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f26f56df1898716dc3a37d357857dcf9418709d826a5efdbf7d46f03bac29fcb
4
- data.tar.gz: efb14c911678c9205c54aa06030371a8b0a12944bb93b9e4ba2d83d259321a10
3
+ metadata.gz: 4960df7b832b98970196c71cff95825187bbc49e7457ee13436a5408b841ef96
4
+ data.tar.gz: 3e6bf39db78519fd2a15e405a46d59cc20d0b789581487f968e7b32efd4c173f
5
5
  SHA512:
6
- metadata.gz: ea3c23f42567b578efe70cf266c13202b1c8f51284baabaec040091268ec7b6804939892cf99883dd7601c380efd3eb3ec76092945b89d77b0e412c1f4749b35
7
- data.tar.gz: 3b44e4b1d721374be329e567623e1a0d40918173897ca0b688f1bc4c9d2452791ffce2361c95d51f82fd9b726c1a6be7abb636fd5d4907e334227ab9051cedd8
6
+ metadata.gz: 4f5bf3d8078530a6a28a2f4f34e85f384ea3e452777902831b0bb144d28dfaa7d95c2bf7d8646c4e1eab427d5f3c293ef351eb069ca23f6bebce8a644ba6df85
7
+ data.tar.gz: b6a0eaa250c74167473ade0f3ea77636d19faf3dd0c26a563fb22f3adf9602d98fb159fb352a188ad1abb08f3b5f6c805a1fd5657f4b16e3484b4a8703ed3839
data/README.md CHANGED
@@ -105,24 +105,24 @@ cycle.next # => GET mycycle + SET mycycle 0
105
105
  :one == cycle.value # => GET mycycle
106
106
 
107
107
  enum = Kredis.enum "myenum", values: %w[ one two three ], default: "one"
108
- "one" == enum.value # => GET myenum
108
+ "one" == enum.value # => GET myenum
109
109
  true == enum.one? # => GET myenum
110
110
  enum.value = "two" # => SET myenum "two"
111
111
  "two" == enum.value # => GET myenum
112
112
  enum.value = "four"
113
113
  "two" == enum.value # => GET myenum
114
114
  enum.reset # => DEL myenum
115
- "one" == enum.value # => GET myenum
115
+ "one" == enum.value # => GET myenum
116
116
 
117
117
  slots = Kredis.slots "myslots", available: 3
118
118
  true == slots.available? # => GET myslots
119
119
  slots.reserve # => INCR myslots
120
120
  true == slots.available? # => GET myslots
121
- slots.reserve # => INCR myslots
121
+ slots.reserve # => INCR myslots
122
122
  true == slots.available? # => GET myslots
123
123
  slots.reserve # => INCR myslots
124
124
  false == slots.available? # => GET myslots
125
- slots.reserve # => INCR myslots + DECR myslots
125
+ slots.reserve # => INCR myslots + DECR myslots
126
126
  false == slots.available? # => GET myslots
127
127
  slots.release # => DECR myslots
128
128
  true == slots.available? # => GET myslots
@@ -141,10 +141,11 @@ flag = Kredis.flag "myflag"
141
141
  false == flag.marked? # => EXISTS myflag
142
142
  flag.mark # => SET myflag 1
143
143
  true == flag.marked? # => EXISTS myflag
144
- flag.remove # => DEL myflag
144
+ flag.remove # => DEL myflag
145
145
  false == flag.marked? # => EXISTS myflag
146
146
 
147
- flag.mark(expires_in: 1.second) #=> SET myflag 1 EX 1
147
+ true == flag.mark(expires_in: 1.second, force: false) #=> SET myflag 1 EX 1 NX
148
+ false == flag.mark(expires_in: 10.seconds, force: false) #=> SET myflag 10 EX 1 NX
148
149
  true == flag.marked? #=> EXISTS myflag
149
150
  sleep 0.5.seconds
150
151
  true == flag.marked? #=> EXISTS myflag
@@ -170,13 +171,14 @@ class Person < ApplicationRecord
170
171
  kredis_list :names_with_custom_key, key: ->(p) { "person:#{p.id}:names_customized" }
171
172
  kredis_unique_list :skills, limit: 2
172
173
  kredis_enum :morning, values: %w[ bright blue black ], default: "bright"
174
+ kredis_counter :steps, expires_in: 1.hour
173
175
  end
174
176
 
175
177
  person = Person.find(5)
176
178
  person.names.append "David", "Heinemeier", "Hansson" # => RPUSH people:5:names "David" "Heinemeier" "Hansson"
177
- true == person.morning.bright? # => GET people:1:morning
178
- person.morning.value = "blue" # => SET people:1:morning
179
- true == person.morning.blue? # => GET people:1:morning
179
+ true == person.morning.bright? # => GET people:5:morning
180
+ person.morning.value = "blue" # => SET people:5:morning
181
+ true == person.morning.blue? # => GET people:5:morning
180
182
  ```
181
183
 
182
184
  You can also define `after_change` callbacks that trigger on mutations:
@@ -195,26 +197,11 @@ end
195
197
 
196
198
  1. Add the `kredis` gem to your Gemfile: `gem 'kredis'`
197
199
  2. Run `./bin/bundle install`
198
- 3. Add a default configuration under `config/redis/shared.yml`
199
-
200
- A default configuration can look like this for `config/redis/shared.yml`:
201
-
202
- ```yaml
203
- production: &production
204
- host: <%= ENV.fetch("REDIS_SHARED_HOST", "127.0.0.1") %>
205
- port: <%= ENV.fetch("REDIS_SHARED_PORT", "6379") %>
206
- timeout: 1
200
+ 3. Run `./bin/rails kredis:install` to add a default configuration at [`config/redis/shared.yml`](lib/install/shared.yml)
207
201
 
208
- development: &development
209
- host: <%= ENV.fetch("REDIS_SHARED_HOST", "127.0.0.1") %>
210
- port: <%= ENV.fetch("REDIS_SHARED_PORT", "6379") %>
211
- timeout: 1
202
+ Additional configurations can be added under `config/redis/*.yml` and referenced when a type is created. For example, `Kredis.string("mystring", config: :strings)` would lookup `config/redis/strings.yml`.
212
203
 
213
- test:
214
- <<: *development
215
- ```
216
-
217
- Additional configurations can be added under `config/redis/*.yml` and referenced when a type is created, e.g. `Kredis.string("mystring", config: :strings)` would lookup `config/redis/strings.yml`. Under the hood `Kredis.configured_for` is called which'll pass the configuration on to `Redis.new`.
204
+ Kredis passes the configuration to `Redis.new` to establish the connection. See the [Redis documentation](https://github.com/redis/redis-rb) for other configuration options.
218
205
 
219
206
  ### Setting SSL options on Redis Connections
220
207
 
@@ -243,6 +230,16 @@ Kredis::Connections.connections[:shared] = Redis.new(
243
230
 
244
231
  The above code could be added to either `config/environments/production.rb` or an initializer. Please ensure that your client private key, if used, is stored your credentials file or another secure location.
245
232
 
233
+ ### Configure how the redis client is created
234
+
235
+ You can configure how the redis client is created by setting `config.kredis.connector` in your `application.rb`:
236
+
237
+ ```ruby
238
+ config.kredis.connector = ->(config) { SomeRedisProxy.new(config) }
239
+ ```
240
+
241
+ By default Kredis will use `Redis.new(config)`.
242
+
246
243
  ## License
247
244
 
248
245
  Kredis is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ yaml_path = Rails.root.join("config/redis/shared.yml")
2
+ unless yaml_path.exist?
3
+ say "Adding `config/redis/shared.yml`"
4
+ empty_directory yaml_path.parent.to_s
5
+ copy_file "#{__dir__}/shared.yml", yaml_path
6
+ end
@@ -0,0 +1,15 @@
1
+ production: &production
2
+ url: <%= ENV.fetch("REDIS_URL", "redis://127.0.0.1:6379/0") %>
3
+ timeout: 1
4
+
5
+ development: &development
6
+ url: <%= ENV.fetch("REDIS_URL", "redis://127.0.0.1:6379/0") %>
7
+ timeout: 1
8
+
9
+ # You can also specify host, port, and db instead of url
10
+ # host: <%= ENV.fetch("REDIS_SHARED_HOST", "127.0.0.1") %>
11
+ # port: <%= ENV.fetch("REDIS_SHARED_PORT", "6379") %>
12
+ # db: <%= ENV.fetch("REDIS_SHARED_DB", "11") %>
13
+
14
+ test:
15
+ <<: *development
@@ -6,40 +6,40 @@ module Kredis::Attributes
6
6
  kredis_connection_with __method__, name, key, config: config, after_change: after_change
7
7
  end
8
8
 
9
- def kredis_string(name, key: nil, config: :shared, after_change: nil)
10
- kredis_connection_with __method__, name, key, config: config, after_change: after_change
9
+ def kredis_string(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
10
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
11
11
  end
12
12
 
13
- def kredis_integer(name, key: nil, config: :shared, after_change: nil)
14
- kredis_connection_with __method__, name, key, config: config, after_change: after_change
13
+ def kredis_integer(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
14
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
15
15
  end
16
16
 
17
- def kredis_decimal(name, key: nil, config: :shared, after_change: nil)
18
- kredis_connection_with __method__, name, key, config: config, after_change: after_change
17
+ def kredis_decimal(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
18
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
19
19
  end
20
20
 
21
- def kredis_datetime(name, key: nil, config: :shared, after_change: nil)
22
- kredis_connection_with __method__, name, key, config: config, after_change: after_change
21
+ def kredis_datetime(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
22
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
23
23
  end
24
24
 
25
- def kredis_flag(name, key: nil, config: :shared, after_change: nil)
26
- kredis_connection_with __method__, name, key, config: config, after_change: after_change
25
+ def kredis_flag(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
26
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
27
27
 
28
28
  define_method("#{name}?") do
29
29
  send(name).marked?
30
30
  end
31
31
  end
32
32
 
33
- def kredis_float(name, key: nil, config: :shared, after_change: nil)
34
- kredis_connection_with __method__, name, key, config: config, after_change: after_change
33
+ def kredis_float(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
34
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
35
35
  end
36
36
 
37
37
  def kredis_enum(name, key: nil, values:, default:, config: :shared, after_change: nil)
38
38
  kredis_connection_with __method__, name, key, values: values, default: default, config: config, after_change: after_change
39
39
  end
40
40
 
41
- def kredis_json(name, key: nil, config: :shared, after_change: nil)
42
- kredis_connection_with __method__, name, key, config: config, after_change: after_change
41
+ def kredis_json(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
42
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
43
43
  end
44
44
 
45
45
  def kredis_list(name, key: nil, typed: :string, config: :shared, after_change: nil)
@@ -62,14 +62,18 @@ module Kredis::Attributes
62
62
  kredis_connection_with __method__, name, key, available: available, config: config, after_change: after_change
63
63
  end
64
64
 
65
- def kredis_counter(name, key: nil, config: :shared, after_change: nil)
66
- kredis_connection_with __method__, name, key, config: config, after_change: after_change
65
+ def kredis_counter(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
66
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
67
67
  end
68
68
 
69
69
  def kredis_hash(name, key: nil, typed: :string, config: :shared, after_change: nil)
70
70
  kredis_connection_with __method__, name, key, typed: typed, config: config, after_change: after_change
71
71
  end
72
72
 
73
+ def kredis_boolean(name, key: nil, config: :shared, after_change: nil, expires_in: nil)
74
+ kredis_connection_with __method__, name, key, config: config, after_change: after_change, expires_in: expires_in
75
+ end
76
+
73
77
  private
74
78
  def kredis_connection_with(method, name, key, **options)
75
79
  ivar_symbol = :"@#{name}_#{method}"
@@ -3,11 +3,12 @@ require "redis"
3
3
  module Kredis::Connections
4
4
  mattr_accessor :connections, default: Hash.new
5
5
  mattr_accessor :configurator
6
+ mattr_accessor :connector, default: ->(config) { Redis.new(config) }
6
7
 
7
8
  def configured_for(name)
8
9
  connections[name] ||= begin
9
10
  Kredis.instrument :meta, message: "Connected to #{name}" do
10
- Redis.new configurator.config_for("redis/#{name}")
11
+ connector.call configurator.config_for("redis/#{name}")
11
12
  end
12
13
  end
13
14
  end
@@ -12,6 +12,10 @@ class Kredis::Railtie < ::Rails::Railtie
12
12
  Kredis::LogSubscriber.logger = config.kredis.logger || Rails.logger
13
13
  end
14
14
 
15
+ initializer "kredis.configuration" do
16
+ Kredis::Connections.connector = config.kredis.connector || ->(config) { Redis.new(config) }
17
+ end
18
+
15
19
  initializer "kredis.configurator" do
16
20
  Kredis.configurator = Rails.application
17
21
  end
@@ -26,4 +30,9 @@ class Kredis::Railtie < ::Rails::Railtie
26
30
  include Kredis::Attributes
27
31
  end
28
32
  end
33
+
34
+ rake_tasks do
35
+ path = File.expand_path("..", __dir__)
36
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
37
+ end
29
38
  end
@@ -7,11 +7,12 @@ class Kredis::Types::CallbacksProxy
7
7
  Kredis::Types::Cycle => %i[ next ],
8
8
  Kredis::Types::Enum => %i[ value= reset ],
9
9
  Kredis::Types::Flag => %i[ mark remove ],
10
- Kredis::Types::Hash => %i[ update delete ],
10
+ Kredis::Types::Hash => %i[ update delete []= remove ],
11
11
  Kredis::Types::List => %i[ remove prepend append << ],
12
12
  Kredis::Types::Scalar => %i[ value= clear ],
13
13
  Kredis::Types::Set => %i[ add << remove replace take clear ],
14
- Kredis::Types::Slots => %i[ reserve release reset ]
14
+ Kredis::Types::Slots => %i[ reserve release reset ],
15
+ Kredis::Types::UniqueList => %i[ remove prepend append << ]
15
16
  }
16
17
 
17
18
  def initialize(type, callback)
@@ -1,5 +1,5 @@
1
1
  class Kredis::Types::Counter < Kredis::Types::Proxying
2
- proxying :multi, :set, :incrby, :decrby, :get, :del
2
+ proxying :multi, :set, :incrby, :decrby, :get, :del, :exists?
3
3
 
4
4
  attr_accessor :expires_in
5
5
 
@@ -1,7 +1,7 @@
1
1
  require "active_support/core_ext/object/inclusion"
2
2
 
3
3
  class Kredis::Types::Enum < Kredis::Types::Proxying
4
- proxying :set, :get, :del
4
+ proxying :set, :get, :del, :exists?
5
5
 
6
6
  attr_accessor :values, :default
7
7
 
@@ -1,8 +1,10 @@
1
1
  class Kredis::Types::Flag < Kredis::Types::Proxying
2
2
  proxying :set, :exists?, :del
3
3
 
4
- def mark(expires_in: nil)
5
- set 1, ex: expires_in
4
+ attr_accessor :expires_in
5
+
6
+ def mark(expires_in: nil, force: true)
7
+ set 1, ex: expires_in || self.expires_in, nx: !force
6
8
  end
7
9
 
8
10
  def marked?
@@ -1,7 +1,7 @@
1
1
  require "active_support/core_ext/hash"
2
2
 
3
3
  class Kredis::Types::Hash < Kredis::Types::Proxying
4
- proxying :hget, :hset, :hmget, :hdel, :hgetall, :hkeys, :hvals, :del
4
+ proxying :hget, :hset, :hmget, :hdel, :hgetall, :hkeys, :hvals, :del, :exists?
5
5
 
6
6
  attr_accessor :typed
7
7
 
@@ -28,6 +28,7 @@ class Kredis::Types::Hash < Kredis::Types::Proxying
28
28
  def remove
29
29
  del
30
30
  end
31
+ alias clear remove
31
32
 
32
33
  def entries
33
34
  (hgetall || {}).transform_values { |val| string_to_type(val, typed) }.with_indifferent_access
@@ -1,5 +1,5 @@
1
1
  class Kredis::Types::List < Kredis::Types::Proxying
2
- proxying :lrange, :lrem, :lpush, :rpush
2
+ proxying :lrange, :lrem, :lpush, :rpush, :exists?, :del
3
3
 
4
4
  attr_accessor :typed
5
5
 
@@ -20,4 +20,8 @@ class Kredis::Types::List < Kredis::Types::Proxying
20
20
  rpush types_to_strings(elements, typed) if elements.flatten.any?
21
21
  end
22
22
  alias << append
23
+
24
+ def clear
25
+ del
26
+ end
23
27
  end
@@ -1,5 +1,5 @@
1
1
  class Kredis::Types::Set < Kredis::Types::Proxying
2
- proxying :smembers, :sadd, :srem, :multi, :del, :sismember, :scard, :spop
2
+ proxying :smembers, :sadd, :srem, :multi, :del, :sismember, :scard, :spop, :exists?
3
3
 
4
4
  attr_accessor :typed
5
5
 
@@ -1,7 +1,7 @@
1
1
  class Kredis::Types::Slots < Kredis::Types::Proxying
2
2
  class NotAvailable < StandardError; end
3
3
 
4
- proxying :incr, :decr, :get, :del
4
+ proxying :incr, :decr, :get, :del, :exists?
5
5
 
6
6
  attr_accessor :available
7
7
 
@@ -1,23 +1,29 @@
1
1
  # You'd normally call this a set, but Redis already has another data type for that
2
2
  class Kredis::Types::UniqueList < Kredis::Types::List
3
- proxying :multi, :ltrim
3
+ proxying :multi, :ltrim, :exists?
4
4
 
5
5
  attr_accessor :typed, :limit
6
6
 
7
7
  def prepend(elements)
8
+ elements = Array(elements).uniq
9
+ return if elements.empty?
10
+
8
11
  multi do
9
12
  remove elements
10
13
  super
11
14
  ltrim 0, (limit - 1) if limit
12
- end if Array(elements).flatten.any?
15
+ end
13
16
  end
14
17
 
15
18
  def append(elements)
19
+ elements = Array(elements).uniq
20
+ return if elements.empty?
21
+
16
22
  multi do
17
23
  remove elements
18
24
  super
19
- ltrim (limit - 1), -1 if limit
20
- end if Array(elements).flatten.any?
25
+ ltrim -limit, -1 if limit
26
+ end
21
27
  end
22
28
  alias << append
23
29
  end
data/lib/kredis/types.rb CHANGED
@@ -47,8 +47,8 @@ module Kredis::Types
47
47
  type_from(Cycle, config, key, after_change: after_change, values: values, expires_in: expires_in)
48
48
  end
49
49
 
50
- def flag(key, config: :shared, after_change: nil)
51
- type_from(Flag, config, key, after_change: after_change)
50
+ def flag(key, config: :shared, after_change: nil, expires_in: nil)
51
+ type_from(Flag, config, key, after_change: after_change, expires_in: expires_in)
52
52
  end
53
53
 
54
54
  def enum(key, values:, default:, config: :shared, after_change: nil)
@@ -1,3 +1,3 @@
1
1
  module Kredis
2
- VERSION = "0.3.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,6 @@
1
+ namespace :kredis do
2
+ desc "Install kredis"
3
+ task :install do
4
+ system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/install.rb", __dir__)}"
5
+ end
6
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kredis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Timm Hansen
8
8
  - David Heinemeier Hansson
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-09-06 00:00:00.000000000 Z
12
+ date: 2022-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -53,7 +53,7 @@ dependencies:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: 6.0.0
56
- description:
56
+ description:
57
57
  email: david@hey.com
58
58
  executables: []
59
59
  extensions: []
@@ -61,6 +61,8 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - MIT-LICENSE
63
63
  - README.md
64
+ - lib/install/install.rb
65
+ - lib/install/shared.yml
64
66
  - lib/kredis.rb
65
67
  - lib/kredis/attributes.rb
66
68
  - lib/kredis/connections.rb
@@ -87,11 +89,12 @@ files:
87
89
  - lib/kredis/types/slots.rb
88
90
  - lib/kredis/types/unique_list.rb
89
91
  - lib/kredis/version.rb
92
+ - lib/tasks/kredis/install.rake
90
93
  homepage: https://github.com/rails/kredis
91
94
  licenses:
92
95
  - MIT
93
96
  metadata: {}
94
- post_install_message:
97
+ post_install_message:
95
98
  rdoc_options: []
96
99
  require_paths:
97
100
  - lib
@@ -106,8 +109,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
109
  - !ruby/object:Gem::Version
107
110
  version: '0'
108
111
  requirements: []
109
- rubygems_version: 3.1.4
110
- signing_key:
112
+ rubygems_version: 3.2.32
113
+ signing_key:
111
114
  specification_version: 4
112
115
  summary: Higher-level data structures built on Redis.
113
116
  test_files: []