Class: Tenjin::FileBaseStore

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

Overview

file base data store

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from KeyValueStore

#[], #[]=

Constructor Details

#initialize(root, lifetime = 604800) ⇒ FileBaseStore

60*60*24*7



1139
1140
1141
1142
# File 'lib/tenjin.rb', line 1139

def initialize(root, lifetime=604800)  # = 60*60*24*7
  self.root = root
  self.lifetime = lifetime
end

Instance Attribute Details

#lifetimeObject

Returns the value of attribute lifetime.



1143
1144
1145
# File 'lib/tenjin.rb', line 1143

def lifetime
  @lifetime
end

#rootObject

Returns the value of attribute root.



1143
1144
1145
# File 'lib/tenjin.rb', line 1143

def root
  @root
end

Instance Method Details

#del(key, *options) ⇒ Object



1198
1199
1200
1201
1202
1203
1204
# File 'lib/tenjin.rb', line 1198

def del(key, *options)
  #: delete data file
  #: if data file doesn't exist, don't raise error
  fpath = filepath(key)
  _ignore_not_found_error { File.unlink(fpath) }
  nil
end

#filepath(key) ⇒ Object



1154
1155
1156
1157
# File 'lib/tenjin.rb', line 1154

def filepath(key)
  #return File.join(@root, key.gsub(/[^-.\w\/]/, '_'))
  return "#{@root}/#{key.gsub(/[^-.\w\/]/, '_')}"
end

#get(key, original_timestamp = nil) ⇒ Object



1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
# File 'lib/tenjin.rb', line 1178

def get(key, original_timestamp=nil)
  #: if cache file is not found, return nil
  fpath = filepath(key)
  #return nil unless File.exist?(fpath)
  stat = _ignore_not_found_error { File.stat(fpath) }
  return nil if stat.nil?
  #: if cache file is older than original data, remove it and return nil
  if original_timestamp && stat.ctime < original_timestamp
    del(key)
    return nil
  end
  #: if cache file is expired then remove it and return nil
  if stat.mtime < Time.now
    del(key)
    return nil
  end
  #: return cache file content
  return _ignore_not_found_error { _read_binary(fpath) }
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


1206
1207
1208
1209
# File 'lib/tenjin.rb', line 1206

def has?(key)
  #: if key exists then return true else return false
  return File.exist?(filepath(key))
end

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



1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/tenjin.rb', line 1159

def set(key, value, lifetime=nil)
  #: create directory for cache
  fpath = filepath(key)
  dir = File.dirname(fpath)
  unless File.exist?(dir)
    require 'fileutils' #unless defined?(FileUtils)
    FileUtils.mkdir_p(dir)
  end
  #: create temporary file and rename it to cache file (in order not to flock)
  tmppath = "#{fpath}#{rand().to_s[1,8]}"
  _write_binary(tmppath, value)
  File.rename(tmppath, fpath)
  #: set mtime (which is regarded as cache expired timestamp)
  timestamp = Time.now + (lifetime || @lifetime)
  File.utime(timestamp, timestamp, fpath)
  #: return value
  return value
end