Last active
October 12, 2019 08:17
-
-
Save IvanShamatov/9a8f7334d115be3bde406cad662d9b41 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Component way of adding redis | |
# vendor/dry/system/components.rb | |
Dry::System.register_provider( | |
:persistence, | |
boot_path: Pathname(__dir__).join('components').realpath() | |
) | |
# vendor/dry/system/components/redis.rb | |
Dry::System.register_component(:redis, provider: :persistence) do | |
init do | |
require 'redis' | |
end | |
start do | |
use :logger | |
register(:redis, Redis.new(url: ENV['REDIS_URL'], logger: application[:logger])) | |
end | |
end | |
# system/container.rb | |
require 'dry/system/container' | |
require_relative '../vendor/dry/system/components' | |
class Container < Dry::System::Container | |
boot(:redis, from: :persistence) | |
end |
This file contains hidden or 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
# Plugin way of adding redis | |
# vendor/dry/system/plugins/redis.rb | |
module Dry | |
module System | |
module Plugins | |
module Redis | |
def self.extended(system) | |
system.after(:configure, &:register_redis) | |
super | |
end | |
def register_redis | |
if registered?(:redis) | |
self | |
else | |
register(:redis, Redis.new(url: ENV['REDIS_URL'], logger: config.logger)) | |
end | |
end | |
end | |
end | |
end | |
end | |
# system/container.rb | |
require 'dry/system/container' | |
require_relative '../vendor/dry/system/plugins/redis' | |
class Container < Dry::System::Container | |
use :redis | |
end |
This file contains hidden or 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
# Basic explicit way to add redis | |
# system/boot/redis.rb | |
Container.boot(:redis) do |application| | |
init do | |
require 'redis' | |
end | |
start do | |
use :logger | |
register(:redis, Redis.new(url: ENV['REDIS_URL'], logger: application[:logger])) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment