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



1150
1151
1152
1153
# File 'lib/tenjin.rb', line 1150

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.



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

def lifetime
  @lifetime
end

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#del(key, *options) ⇒ Object



1209
1210
1211
1212
1213
1214
1215
# File 'lib/tenjin.rb', line 1209

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



1165
1166
1167
1168
# File 'lib/tenjin.rb', line 1165

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

#get(key, original_timestamp = nil) ⇒ Object



1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
# File 'lib/tenjin.rb', line 1189

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)


1217
1218
1219
1220
# File 'lib/tenjin.rb', line 1217

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

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



1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'lib/tenjin.rb', line 1170

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