-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 40926a4
Showing
21 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source "https://rubygems.org" | ||
|
||
gemspec | ||
|
||
gem "rake" | ||
gem "byebug" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
kredis (0.1.0) | ||
rails (>= 6.0.0) | ||
redis (~> 4.0) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
actioncable (6.1.1) | ||
actionpack (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
nio4r (~> 2.0) | ||
websocket-driver (>= 0.6.1) | ||
actionmailbox (6.1.1) | ||
actionpack (= 6.1.1) | ||
activejob (= 6.1.1) | ||
activerecord (= 6.1.1) | ||
activestorage (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
mail (>= 2.7.1) | ||
actionmailer (6.1.1) | ||
actionpack (= 6.1.1) | ||
actionview (= 6.1.1) | ||
activejob (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
mail (~> 2.5, >= 2.5.4) | ||
rails-dom-testing (~> 2.0) | ||
actionpack (6.1.1) | ||
actionview (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
rack (~> 2.0, >= 2.0.9) | ||
rack-test (>= 0.6.3) | ||
rails-dom-testing (~> 2.0) | ||
rails-html-sanitizer (~> 1.0, >= 1.2.0) | ||
actiontext (6.1.1) | ||
actionpack (= 6.1.1) | ||
activerecord (= 6.1.1) | ||
activestorage (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
nokogiri (>= 1.8.5) | ||
actionview (6.1.1) | ||
activesupport (= 6.1.1) | ||
builder (~> 3.1) | ||
erubi (~> 1.4) | ||
rails-dom-testing (~> 2.0) | ||
rails-html-sanitizer (~> 1.1, >= 1.2.0) | ||
activejob (6.1.1) | ||
activesupport (= 6.1.1) | ||
globalid (>= 0.3.6) | ||
activemodel (6.1.1) | ||
activesupport (= 6.1.1) | ||
activerecord (6.1.1) | ||
activemodel (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
activestorage (6.1.1) | ||
actionpack (= 6.1.1) | ||
activejob (= 6.1.1) | ||
activerecord (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
marcel (~> 0.3.1) | ||
mimemagic (~> 0.3.2) | ||
activesupport (6.1.1) | ||
concurrent-ruby (~> 1.0, >= 1.0.2) | ||
i18n (>= 1.6, < 2) | ||
minitest (>= 5.1) | ||
tzinfo (~> 2.0) | ||
zeitwerk (~> 2.3) | ||
builder (3.2.4) | ||
byebug (11.1.3) | ||
concurrent-ruby (1.1.8) | ||
crass (1.0.6) | ||
erubi (1.10.0) | ||
globalid (0.4.2) | ||
activesupport (>= 4.2.0) | ||
i18n (1.8.7) | ||
concurrent-ruby (~> 1.0) | ||
loofah (2.9.0) | ||
crass (~> 1.0.2) | ||
nokogiri (>= 1.5.9) | ||
mail (2.7.1) | ||
mini_mime (>= 0.1.1) | ||
marcel (0.3.3) | ||
mimemagic (~> 0.3.2) | ||
method_source (1.0.0) | ||
mimemagic (0.3.5) | ||
mini_mime (1.0.2) | ||
minitest (5.14.3) | ||
nio4r (2.5.4) | ||
nokogiri (1.11.1-x86_64-darwin) | ||
racc (~> 1.4) | ||
racc (1.5.2) | ||
rack (2.2.3) | ||
rack-test (1.1.0) | ||
rack (>= 1.0, < 3) | ||
rails (6.1.1) | ||
actioncable (= 6.1.1) | ||
actionmailbox (= 6.1.1) | ||
actionmailer (= 6.1.1) | ||
actionpack (= 6.1.1) | ||
actiontext (= 6.1.1) | ||
actionview (= 6.1.1) | ||
activejob (= 6.1.1) | ||
activemodel (= 6.1.1) | ||
activerecord (= 6.1.1) | ||
activestorage (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
bundler (>= 1.15.0) | ||
railties (= 6.1.1) | ||
sprockets-rails (>= 2.0.0) | ||
rails-dom-testing (2.0.3) | ||
activesupport (>= 4.2.0) | ||
nokogiri (>= 1.6) | ||
rails-html-sanitizer (1.3.0) | ||
loofah (~> 2.3) | ||
railties (6.1.1) | ||
actionpack (= 6.1.1) | ||
activesupport (= 6.1.1) | ||
method_source | ||
rake (>= 0.8.7) | ||
thor (~> 1.0) | ||
rake (13.0.3) | ||
redis (4.2.5) | ||
sprockets (4.0.2) | ||
concurrent-ruby (~> 1.0) | ||
rack (> 1, < 3) | ||
sprockets-rails (3.2.2) | ||
actionpack (>= 4.0) | ||
activesupport (>= 4.0) | ||
sprockets (>= 3.0.0) | ||
thor (1.1.0) | ||
tzinfo (2.0.4) | ||
concurrent-ruby (~> 1.0) | ||
websocket-driver (0.7.3) | ||
websocket-extensions (>= 0.1.0) | ||
websocket-extensions (0.1.5) | ||
zeitwerk (2.4.2) | ||
|
||
PLATFORMS | ||
ruby | ||
x86_64-darwin-20 | ||
|
||
DEPENDENCIES | ||
byebug | ||
kredis! | ||
rake | ||
|
||
BUNDLED WITH | ||
2.2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2020 Basecamp | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Kredis | ||
|
||
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. | ||
|
||
|
||
## Development | ||
|
||
... | ||
|
||
## License | ||
|
||
Kredis is released under the [MIT License](https://opensource.org/licenses/MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "bundler/setup" | ||
require "bundler/gem_tasks" | ||
require "rake/testtask" | ||
|
||
Rake::TestTask.new do |test| | ||
test.libs << "test" | ||
test.test_files = FileList["test/**/*_test.rb"] | ||
end | ||
|
||
task default: :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require_relative "lib/kredis/version" | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "kredis" | ||
s.version = Kredis::VERSION | ||
s.authors = [ "Kasper Timm Hansen", "David Heinemeier Hansson" ] | ||
s.email = "[email protected]" | ||
s.summary = "Higher-level data structures built on Redis." | ||
s.homepage = "https://github.com/rails/kredis" | ||
s.license = "MIT" | ||
|
||
s.required_ruby_version = ">= 2.6.0" | ||
s.add_dependency "rails", ">= 6.0.0" | ||
s.add_dependency "redis", "~> 4.0" | ||
|
||
s.files = Dir["lib/**/*", "MIT-LICENSE", "README.md"] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "kredis/railtie" | ||
|
||
require "kredis/connections" | ||
require "kredis/types" | ||
require "kredis/attributes" | ||
require "kredis/namespace" | ||
|
||
module Kredis | ||
include Connections | ||
include Namespace | ||
include Types | ||
|
||
extend self | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Kredis::Attributes | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def redis_list(name, config: :shared) | ||
ivar_symbol = :"@#{name}_redis_list" | ||
|
||
define_method(name) do | ||
if instance_variable_defined?(ivar_symbol) | ||
instance_variable_get(ivar_symbol) | ||
else | ||
instance_variable_set(ivar_symbol, Kredis.list(redis_key_for_attribute(name), config: config)) | ||
end | ||
end | ||
end | ||
|
||
def redis_unique_list(name, limit: nil, config: :shared) | ||
ivar_symbol = :"@#{name}_redis_unique_list" | ||
|
||
define_method(name) do | ||
if instance_variable_defined?(ivar_symbol) | ||
instance_variable_get(ivar_symbol) | ||
else | ||
instance_variable_set(ivar_symbol, Kredis.unique_list(redis_key_for_attribute(name), limit: limit, config: config)) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
def redis_key_for_attribute(name) | ||
"#{self.class.name.tableize.gsub("/", ":")}:#{id}:#{name}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "redis" | ||
|
||
module Kredis::Connections | ||
mattr_accessor :connections, default: Hash.new | ||
mattr_accessor :configurator, default: Rails.application | ||
|
||
def configured_for(name) | ||
connections[name] ||= Redis.new configurator.config_for("redis/#{name}") | ||
end | ||
|
||
def clear_all | ||
connections.each_value(&:flushdb) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Kredis::Namespace | ||
def namespace=(namespace) | ||
Thread.current[:kredis_namespace] = namespace | ||
end | ||
|
||
def namespace | ||
Thread.current[:kredis_namespace] | ||
end | ||
|
||
def namespaced_key(key) | ||
namespace ? "#{namespace}:#{key}" : key | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Kredis::Proxy | ||
def initialize(redis, key) | ||
@redis, @key = redis, key | ||
end | ||
|
||
def multi(...) | ||
@redis.multi(...) | ||
end | ||
|
||
def method_missing(method, *args, **kwargs) | ||
@redis.public_send method, @key, *args, **kwargs | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "rails/railtie" | ||
|
||
module Kredis | ||
class Railtie < ::Rails::Railtie | ||
config.kredis = ActiveSupport::OrderedOptions.new | ||
config.eager_load_namespaces << Kredis | ||
|
||
initializer "kredis.testing" do | ||
ActiveSupport.on_load(:active_support_test_case) do | ||
parallelize_setup { |worker| Kredis.namespace = "test-#{worker}" } | ||
parallelize_teardown { Kredis.clear_all } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Kredis::Types | ||
def keyed(key, config: :shared) | ||
Proxy.new configured_for(config), namespaced_key(key) | ||
end | ||
|
||
def list(key, config: :shared) | ||
List.new configured_for(config), namespaced_key(key) | ||
end | ||
|
||
def unique_list(key, limit: nil, config: :shared) | ||
UniqueList.new configured_for(config), namespaced_key(key), limit: limit | ||
end | ||
|
||
def counter(key, expires_in: nil, config: :shared) | ||
Counter.new configured_for(config), namespaced_key(key), expires_in: expires_in | ||
end | ||
|
||
def mutex(key, expires_in: nil, config: :shared) | ||
Mutex.new configured_for(config), namespaced_key(key), expires_in: expires_in | ||
end | ||
end | ||
|
||
require "kredis/proxy" | ||
require "kredis/types/list" | ||
require "kredis/types/unique_list" | ||
require "kredis/types/counter" | ||
require "kredis/types/mutex" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Kredis::Types::Counter < Kredis::Proxy | ||
def initialize(redis, key, expires_in: nil) | ||
@expires_in = expires_in | ||
super redis, key | ||
end | ||
|
||
def increment(by: 1) | ||
multi do | ||
set 0, ex: @expires_in, nx: true | ||
incrby by | ||
end | ||
end | ||
|
||
def value | ||
get.to_i | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Kredis::Types::List < Kredis::Proxy | ||
def elements | ||
lrange(0, -1) || [] | ||
end | ||
|
||
def remove(elements) | ||
Array(elements).each { |element| lrem 0, element } | ||
end | ||
|
||
def prepend(elements) | ||
lpush elements | ||
end | ||
|
||
def append(elements) | ||
rpush elements | ||
end | ||
end |
Oops, something went wrong.