kredis 0.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.

@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a6a806a43280483f33a61cb2133b869d7742251eb7df3ccfcfe18c4bc5274873
4
+ data.tar.gz: 1c255fa02c7172090724ee01b5f72ec5540b4e38c43505135bd123c2ab6a1c79
5
+ SHA512:
6
+ metadata.gz: 270f4fda9ed70fb489e9b54431ae01118f2f25fcb6632a8eb83abb50e4ff0871729a4282989c75801bd6f0c7520f72d51923b6980c46089f0f27a3d8145a2a6e
7
+ data.tar.gz: d1d5cc05fbe9091db57a20ad138e08cf1ee60d00765ebf69222cf53f9c153ce0a260496d0dd406c807ef1b6b51164ab365487c72e9299ca703d776add411c564
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2020 Basecamp
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.
@@ -0,0 +1,12 @@
1
+ # Kredis
2
+
3
+ Keyed Redis encapsulates higher-level data structures around a single key, so you can interact with them as coherent objects rather than isolated procedural commands.
4
+
5
+
6
+ ## Development
7
+
8
+ ...
9
+
10
+ ## License
11
+
12
+ Kredis is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,14 @@
1
+ require "kredis/railtie"
2
+
3
+ require "kredis/connections"
4
+ require "kredis/types"
5
+ require "kredis/attributes"
6
+ require "kredis/namespace"
7
+
8
+ module Kredis
9
+ include Connections
10
+ include Namespace
11
+ include Types
12
+
13
+ extend self
14
+ end
@@ -0,0 +1,34 @@
1
+ module Kredis::Attributes
2
+ extend ActiveSupport::Concern
3
+
4
+ class_methods do
5
+ def kredis_list(name, config: :shared)
6
+ ivar_symbol = :"@#{name}_kredis_list"
7
+
8
+ define_method(name) do
9
+ if instance_variable_defined?(ivar_symbol)
10
+ instance_variable_get(ivar_symbol)
11
+ else
12
+ instance_variable_set(ivar_symbol, Kredis.list(kredis_key_for_attribute(name), config: config))
13
+ end
14
+ end
15
+ end
16
+
17
+ def kredis_unique_list(name, limit: nil, config: :shared)
18
+ ivar_symbol = :"@#{name}_kredis_unique_list"
19
+
20
+ define_method(name) do
21
+ if instance_variable_defined?(ivar_symbol)
22
+ instance_variable_get(ivar_symbol)
23
+ else
24
+ instance_variable_set(ivar_symbol, Kredis.unique_list(kredis_key_for_attribute(name), limit: limit, config: config))
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+ def kredis_key_for_attribute(name)
32
+ "#{self.class.name.tableize.gsub("/", ":")}:#{id}:#{name}"
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ require "redis"
2
+
3
+ module Kredis::Connections
4
+ mattr_accessor :connections, default: Hash.new
5
+ mattr_accessor :configurator
6
+
7
+ def configured_for(name)
8
+ connections[name] ||= Redis.new configurator.config_for("redis/#{name}")
9
+ end
10
+
11
+ def clear_all
12
+ connections.each_value(&:flushdb)
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ module Kredis::Namespace
2
+ def namespace=(namespace)
3
+ Thread.current[:kredis_namespace] = namespace
4
+ end
5
+
6
+ def namespace
7
+ Thread.current[:kredis_namespace]
8
+ end
9
+
10
+ def namespaced_key(key)
11
+ namespace ? "#{namespace}:#{key}" : key
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class Kredis::Proxy
2
+ def initialize(redis, key)
3
+ @redis, @key = redis, key
4
+ end
5
+
6
+ def multi(...)
7
+ @redis.multi(...)
8
+ end
9
+
10
+ def method_missing(method, *args, **kwargs)
11
+ @redis.public_send method, @key, *args, **kwargs
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ require "rails/railtie"
2
+
3
+ module Kredis
4
+ class Railtie < ::Rails::Railtie
5
+ config.kredis = ActiveSupport::OrderedOptions.new
6
+ config.eager_load_namespaces << Kredis
7
+
8
+ initializer "kredis.testing" do
9
+ ActiveSupport.on_load(:active_support_test_case) do
10
+ parallelize_setup { |worker| Kredis.namespace = "test-#{worker}" }
11
+ parallelize_teardown { Kredis.clear_all }
12
+ end
13
+ end
14
+
15
+ initializer "kredis.configurator" do
16
+ Kredis.configurator = Rails.application
17
+ end
18
+
19
+ initializer "kredis.attributes" do
20
+ ActiveSupport.on_load(:active_model) do
21
+ ActiveModel::Base.send :include, Kredis::Attributes
22
+ end
23
+
24
+ ActiveSupport.on_load(:active_record) do
25
+ ActiveRecord::Base.send :include, Kredis::Attributes
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ module Kredis::Types
2
+ def keyed(key, config: :shared)
3
+ Kredis::Proxy.new configured_for(config), namespaced_key(key)
4
+ end
5
+
6
+ def list(key, config: :shared)
7
+ List.new configured_for(config), namespaced_key(key)
8
+ end
9
+
10
+ def unique_list(key, limit: nil, config: :shared)
11
+ UniqueList.new configured_for(config), namespaced_key(key), limit: limit
12
+ end
13
+
14
+ def counter(key, expires_in: nil, config: :shared)
15
+ Counter.new configured_for(config), namespaced_key(key), expires_in: expires_in
16
+ end
17
+
18
+ def mutex(key, expires_in: nil, config: :shared)
19
+ Mutex.new configured_for(config), namespaced_key(key), expires_in: expires_in
20
+ end
21
+ end
22
+
23
+ require "kredis/proxy"
24
+ require "kredis/types/list"
25
+ require "kredis/types/unique_list"
26
+ require "kredis/types/counter"
27
+ require "kredis/types/mutex"
@@ -0,0 +1,17 @@
1
+ class Kredis::Types::Counter < Kredis::Proxy
2
+ def initialize(redis, key, expires_in: nil)
3
+ @expires_in = expires_in
4
+ super redis, key
5
+ end
6
+
7
+ def increment(by: 1)
8
+ multi do
9
+ set 0, ex: @expires_in, nx: true
10
+ incrby by
11
+ end
12
+ end
13
+
14
+ def value
15
+ get.to_i
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ class Kredis::Types::List < Kredis::Proxy
2
+ def elements
3
+ lrange(0, -1) || []
4
+ end
5
+
6
+ def remove(elements)
7
+ Array(elements).each { |element| lrem 0, element }
8
+ end
9
+
10
+ def prepend(elements)
11
+ lpush elements
12
+ end
13
+
14
+ def append(elements)
15
+ rpush elements
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ class Kredis::Types::Mutex < Kredis::Proxy
2
+ def initialize(redis, key, expires_in: nil)
3
+ @expires_in = expires_in
4
+ super redis, key
5
+ end
6
+
7
+ def lock
8
+ set 1, ex: @expires_in
9
+ end
10
+
11
+ def unlock
12
+ del
13
+ end
14
+
15
+ def locked?
16
+ get
17
+ end
18
+
19
+ def synchronize
20
+ lock
21
+ yield
22
+ ensure
23
+ unlock
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # You'd normally call this a set, but Redis already has another data type for that
2
+ class Kredis::Types::UniqueList < Kredis::Types::List
3
+ def initialize(redis, key, limit: nil)
4
+ @limit = limit
5
+ super redis, key
6
+ end
7
+
8
+ def prepend(elements)
9
+ multi do
10
+ remove elements
11
+ super
12
+ ltrim 0, (@limit - 1) if @limit
13
+ end
14
+ end
15
+
16
+ def append(elements)
17
+ multi do
18
+ remove elements
19
+ super
20
+ ltrim (@limit - 1), -1 if @limit
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Kredis
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kredis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Timm Hansen
8
+ - David Heinemeier Hansson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-01-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 6.0.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 6.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: redis
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '4.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '4.0'
42
+ description:
43
+ email: david@hey.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - lib/kredis.rb
51
+ - lib/kredis/attributes.rb
52
+ - lib/kredis/connections.rb
53
+ - lib/kredis/namespace.rb
54
+ - lib/kredis/proxy.rb
55
+ - lib/kredis/railtie.rb
56
+ - lib/kredis/types.rb
57
+ - lib/kredis/types/counter.rb
58
+ - lib/kredis/types/list.rb
59
+ - lib/kredis/types/mutex.rb
60
+ - lib/kredis/types/unique_list.rb
61
+ - lib/kredis/version.rb
62
+ homepage: https://github.com/rails/kredis
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.6.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.1.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Higher-level data structures built on Redis.
85
+ test_files: []