Class: Tenjin::MemoryBaseStore

Inherits:
KeyValueStore show all
Defined in:
lib/tenjin.rb

Overview

memory base data store

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from KeyValueStore

#[], #[]=

Constructor Details

#initialize(lifetime = 604800) ⇒ MemoryBaseStore

Returns a new instance of MemoryBaseStore.



1100
1101
1102
1103
# File 'lib/tenjin.rb', line 1100

def initialize(lifetime=604800)
  @values = {}
  @lifetime = lifetime
end

Instance Attribute Details

#lifetimeObject

Returns the value of attribute lifetime.



1104
1105
1106
# File 'lib/tenjin.rb', line 1104

def lifetime
  @lifetime
end

#valuesObject

Returns the value of attribute values.



1104
1105
1106
# File 'lib/tenjin.rb', line 1104

def values
  @values
end

Instance Method Details

#del(key) ⇒ Object



1131
1132
1133
1134
1135
# File 'lib/tenjin.rb', line 1131

def del(key)
  #: remove data
  #: don't raise error even if key doesn't exist
  @values.delete(key)
end

#get(key, original_timestamp = nil) ⇒ Object



1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
# File 'lib/tenjin.rb', line 1112

def get(key, original_timestamp=nil)
  #: if cache data is not found, return nil
  arr = @values[key]
  return nil if arr.nil?
  #: if cache data is older than original data, remove it and return nil
  value, created_at, timestamp = arr
  if original_timestamp && created_at < original_timestamp
    del(key)
    return nil
  end
  #: if cache data is expired then remove it and return nil
  if timestamp < Time.now
    del(key)
    return nil
  end
  #: return cache data
  return value
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


1137
1138
1139
1140
# File 'lib/tenjin.rb', line 1137

def has?(key)
  #: if key exists then return true else return false
  return @values.key?(key)
end

#set(key, value, lifetime = nil) ⇒ Object



1106
1107
1108
1109
1110
# File 'lib/tenjin.rb', line 1106

def set(key, value, lifetime=nil)
  #: store key and value with current and expired timestamp
  now = Time.now
  @values[key] = [value, now, now + (lifetime || @lifetime)]
end