Class: TravelAdapter::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/traveladapter/adapter.rb

Overview

Versatile wrapper around key-value stores, providing abstraction and expiration facility.

Instance Method Summary collapse

Constructor Details

#initialize(store = nil) ⇒ Adapter

Create a new instance of TravelAdapter::Adapter object.

If no object is passed, the Adapter will internally use a Hash.

Parameters:

  • store (Object) (defaults to: nil)

    Underlying key-value store (eg. Hash, Redis, LevelDB etc.)



15
16
17
18
19
20
21
22
# File 'lib/traveladapter/adapter.rb', line 15

def initialize(store=nil)
  @semaphore = Mutex::new
  if store.nil?
    @store = Hash::new
  else
    @store = store
  end
end

Instance Method Details

#decrement(key) ⇒ Numeric

Decrement a counter stored under a particular key. A counter can be any numeric object, as long as it responds sensibly to + and - messages.

Parameters:

  • key (String)

    Key used to find a counter object.

Returns:

  • (Numeric)

    Counter value after decrementing.



30
31
32
# File 'lib/traveladapter/adapter.rb', line 30

def decrement(key)
  change_by(key, -1)
end

#delete(key) ⇒ Boolean

Delete a key-value pair stored with a particular key.

Parameters:

  • key (String)

    Key used to find a counter object.

Returns:

  • (Boolean)

    true if the operation succeeds, false if not



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/traveladapter/adapter.rb', line 38

def delete(key)
  begin
    @semaphore.synchronize do
      atomic_delete(key)
      atomic_delete(marshal_key(key))
      atomic_delete(expiration_key(key))
    end
    return true
  rescue Exception
    return false
  end
end

#get(key) ⇒ Object Also known as: []

Get the value stored with a particular key.

Parameters:

  • key (String)

    Key used to find the value.

Returns:

  • (Object)

    object stored with a given key. Returns nil if the object is not found or the key has expired.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/traveladapter/adapter.rb', line 56

def get(key)
  return nil unless atomic_has_key?(key)
  if expired?(key)
    delete(key)
    return nil
  elsif needs_marshaling?(key)
    return Marshal::load(atomic_get(key))
  else
    return atomic_get(key)
  end
end

#get_ttl(key) ⇒ Integer

Get expiration time.

Parameters:

  • key (String)

    Key used to for the object.

Returns:

  • (Integer)

    Number of seconds until object expiration



133
134
135
136
# File 'lib/traveladapter/adapter.rb', line 133

def get_ttl(key)
  expires = atomic_get(expiration_key(key))
  return expires.nil? ? nil : (expires.to_i - Time::now.to_i)
end

#has_key?(key) ⇒ Boolean

Check if anything is stored with a particular key.

a value, otherwise false.

Parameters:

  • key (String)

    Key used to find a counter object.

Returns:

  • (Boolean)

    true if the key is not expired and associated with



73
74
75
# File 'lib/traveladapter/adapter.rb', line 73

def has_key?(key)
  !get(key).nil?
end

#increment(key) ⇒ Numeric

Increment a counter stored under a particular key. A counter can be any numeric object, as long as it responds sensibly to + and - messages.

Parameters:

  • key (String)

    Key used to find a counter object.

Returns:

  • (Numeric)

    Counter value after incrementing.



83
84
85
# File 'lib/traveladapter/adapter.rb', line 83

def increment(key)
  change_by(key, 1)
end

#keys(pattern = /.*/) ⇒ Array

Get non-expired keys stored.

Parameters:

  • pattern (Regexp) (defaults to: /.*/)

    Regular expression used to filter out keys.

Returns:

  • (Array)

    Non-expired keys matching the given pattern.



91
92
93
94
95
96
# File 'lib/traveladapter/adapter.rb', line 91

def keys(pattern=/.*/)
  re = /\A__(expire|marshal)__:/
  return atomic_keys.select do |k|
    k.match(re).nil? and not expired?(k) and !k.match(pattern).nil?
  end
end

#set(key, value, ttl = (10 << 32)) ⇒ Object Also known as: []=

Set the value stored with a particular key.

Parameters:

  • key (String)

    Key used to for the object.

  • value (String)

    Value for the key.

  • ttl (Integer) (defaults to: (10 << 32))

    Time (in seconds) to keep the value.

Returns:

  • (Object)

    object stored.



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/traveladapter/adapter.rb', line 104

def set(key, value, ttl=(10 << 32))
  @semaphore.synchronize do
    if value.is_a?(String)
      atomic_set(marshal_key(key), "0")
    else
      atomic_set(marshal_key(key), "1")
      value = Marshal::dump(value)
    end
    atomic_set(key, value)
    set_ttl(key, ttl)
  end
  return value
end

#set_ttl(key, ttl) ⇒ Time

Change how long the value is to be stored.

Parameters:

  • key (String)

    Key used to for the object.

  • ttl (Integer)

    Time from now (in seconds) to keep the value.

Returns:

  • (Time)

    Time when the key expires.



123
124
125
126
127
# File 'lib/traveladapter/adapter.rb', line 123

def set_ttl(key, ttl)
  new_ttl = Time::now.to_i + ttl
  atomic_set(expiration_key(key), new_ttl.to_s)
  return Time::at(new_ttl)
end