Skip to content

Commit

Permalink
First
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Jan 30, 2021
0 parents commit 40926a4
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

gemspec

gem "rake"
gem "byebug"
150 changes: 150 additions & 0 deletions Gemfile.lock
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
20 changes: 20 additions & 0 deletions MIT-LICENSE
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.
12 changes: 12 additions & 0 deletions README.md
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).
10 changes: 10 additions & 0 deletions Rakefile
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
17 changes: 17 additions & 0 deletions kredis.gemspec
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
14 changes: 14 additions & 0 deletions lib/kredis.rb
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
34 changes: 34 additions & 0 deletions lib/kredis/attributes.rb
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
14 changes: 14 additions & 0 deletions lib/kredis/connections.rb
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
13 changes: 13 additions & 0 deletions lib/kredis/namespace.rb
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
13 changes: 13 additions & 0 deletions lib/kredis/proxy.rb
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
15 changes: 15 additions & 0 deletions lib/kredis/railtie.rb
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
27 changes: 27 additions & 0 deletions lib/kredis/types.rb
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"
17 changes: 17 additions & 0 deletions lib/kredis/types/counter.rb
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
17 changes: 17 additions & 0 deletions lib/kredis/types/list.rb
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
Loading

0 comments on commit 40926a4

Please sign in to comment.