Class: Y::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/y/redis.rb,
lib/y/redis/config.rb,
lib/y/redis/version.rb,
lib/y/redis/config/option.rb,
lib/y/redis/config/validations.rb,
lib/y/redis/config/abstract_builder.rb

Overview

A Y::Redis instance can be used to persist and load a Doc

Examples:

Sync local to remote document via store

client = Redis.new
lock_manager = Redlock::Client.new

local = Y::Doc.new
local_text = local.get_text("my text")
local_text << "Hello, World!"

store = Y::Redis.new
store.save("my text", doc.diff)

update = store.load("my text")

remote = Y::Doc
remote.sync(update)
remote_text = remote.get_text("my text")
puts remote_text.to_s # "Hello, World!"

Defined Under Namespace

Classes: Config, Error, MissingConfiguration, MissingConfigurationBuilderClass

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, lock_manager) ⇒ Redis

Returns a new instance of Redis.

Parameters:

  • client (::Redis)
  • lock_manager (::Redlock::Client)


40
41
42
43
# File 'lib/y/redis.rb', line 40

def initialize(client, lock_manager)
  @client = client
  @lock_manager = lock_manager
end

Instance Attribute Details

#client::Redis (readonly)

Returns the current value of client.

Returns:

  • (::Redis)

    the current value of client



33
34
35
# File 'lib/y/redis.rb', line 33

def client
  @client
end

#lock_manager::Redlock::Client (readonly)

Returns the current value of lock_manager.

Returns:

  • (::Redlock::Client)

    the current value of lock_manager



33
34
35
# File 'lib/y/redis.rb', line 33

def lock_manager
  @lock_manager
end

Class Method Details

.configurationObject Also known as: config



24
25
26
# File 'lib/y/redis/config.rb', line 24

def configuration
  @config || (raise MissingConfiguration)
end

.configure(&block) ⇒ Object



20
21
22
# File 'lib/y/redis/config.rb', line 20

def configure(&block)
  @config = Config::Builder.new(&block).build
end

Instance Method Details

#load(id) ⇒ ::Array<Integer>?

Load document from store

Parameters:

  • id (String)

Returns:

  • (::Array<Integer>, nil)


67
68
69
70
# File 'lib/y/redis.rb', line 67

def load(id)
  data = client.get(id)
  decode(data)
end

#save(id, update) ⇒ Boolean

Save document to store.

This method expects a binary encoded update in the form of an Array of Integers.

Parameters:

  • id (String)
  • update (::Array<Integer>)

Returns:

  • (Boolean)

    True if lock was acquired and document saved



53
54
55
56
57
58
59
60
61
# File 'lib/y/redis.rb', line 53

def save(id, update)
  lock_manager.lock!("resource_key", 2000) do
    data = encode(update)
    client.set(id, data)
    true
  end
rescue Redlock::LockError
  false
end