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.



1089
1090
1091
1092
# File 'lib/tenjin.rb', line 1089

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

Instance Attribute Details

#lifetimeObject

Returns the value of attribute lifetime.



1093
1094
1095
# File 'lib/tenjin.rb', line 1093

def lifetime
  @lifetime
end

#valuesObject

Returns the value of attribute values.



1093
1094
1095
# File 'lib/tenjin.rb', line 1093

def values
  @values
end

Instance Method Details

#del(key) ⇒ Object



1120
1121
1122
1123
1124
# File 'lib/tenjin.rb', line 1120

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



1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/tenjin.rb', line 1101

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)


1126
1127
1128
1129
# File 'lib/tenjin.rb', line 1126

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

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



1095
1096
1097
1098
1099
# File 'lib/tenjin.rb', line 1095

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