cache-storage
Version:
[ABANDONED] Advanced cache storage for node js
172 lines (127 loc) • 4.46 kB
text/coffeescript
isWindow = if typeof window == 'undefined' then false else true
if !isWindow
path = require 'path'
BaseStorage = require '../Storage'
moment = require 'moment'
Cache = require '../../Cache'
class Storage extends BaseStorage
async: false
read: (key) ->
data =
if typeof data[key] == 'undefined'
return null
else
if
return data[key]
else
return null
write: (key, data, dependencies = {}) ->
all =
all[key] = data
meta =
meta[key] = dependencies
remove: (key) ->
data =
meta =
if typeof data[key] != 'undefined'
delete data[key]
delete meta[key]
removeAll: ->
clean: (conditions) ->
typeFn = Object.prototype.toString
type = typeFn.call(conditions)
if conditions == Cache.ALL
else if type == '[object Object]'
if typeof conditions[Cache.TAGS] != 'undefined'
if typeFn(conditions[Cache.TAGS]) == '[object String]' then conditions[Cache.TAGS] = [conditions[Cache.TAGS]]
for tag in conditions[Cache.TAGS]
for key in
if typeof conditions[Cache.PRIORITY] != 'undefined'
for key in
findMeta: (key) ->
meta =
return if typeof meta[key] != 'undefined' then meta[key] else null
findKeysByTag: (tag) ->
metas =
result = []
for key, meta of metas
if typeof meta[Cache.TAGS] != 'undefined' && meta[Cache.TAGS].indexOf(tag) != -1
result.push(key)
return result
findKeysByPriority: (priority) ->
metas =
result = []
for key, meta of metas
if typeof meta[Cache.PRIORITY] != 'undefined' && meta[Cache.PRIORITY] <= priority
result.push(key)
return result
verify: (meta) ->
typefn = Object.prototype.toString
if typefn.call(meta) == '[object Object]'
if typeof meta[Cache.EXPIRE] != 'undefined'
if moment().valueOf() >= meta[Cache.EXPIRE]
return false
if typeof meta[Cache.ITEMS] != 'undefined'
for item in meta[Cache.ITEMS]
item =
if (item == null) || (item != null && == false)
return false
if typeof meta[Cache.FILES] != 'undefined'
if isWindow
for file, time of meta[Cache.FILES]
mtime = window.require.getStats(file).mtime
if mtime == null
throw new Error 'File stats are disabled in your simq configuration. Can not get stats for ' + file + '.'
if window.require.getStats(file).mtime.getTime() != time
return false
else
for file, time of meta[Cache.FILES]
if (new Date(Cache.getFs().statSync(file).mtime)).getTime() != time
return false
return true
parseDependencies: (dependencies) ->
typefn = Object.prototype.toString
result = {}
if typefn.call(dependencies) == '[object Object]'
if typeof dependencies[Cache.PRIORITY] != 'undefined'
result[Cache.PRIORITY] = dependencies[Cache.PRIORITY]
if typeof dependencies[Cache.TAGS] != 'undefined'
result[Cache.TAGS] = dependencies[Cache.TAGS]
if typeof dependencies[Cache.ITEMS] != 'undefined'
result[Cache.ITEMS] = []
for item in dependencies[Cache.ITEMS]
result[Cache.ITEMS].push(.generateKey(item))
if typeof dependencies[Cache.EXPIRE] != 'undefined'
switch typefn.call(dependencies[Cache.EXPIRE])
when '[object String]'
time = moment(dependencies[Cache.EXPIRE], Cache.TIME_FORMAT)
when '[object Object]'
time = moment().add(dependencies[Cache.EXPIRE])
else
throw new Error 'Expire format is not valid'
result[Cache.EXPIRE] = time.valueOf()
if typeof dependencies[Cache.FILES] != 'undefined'
files = {}
if isWindow
for file in dependencies[Cache.FILES]
mtime = window.require.getStats(file).mtime
if mtime == null
throw new Error 'File stats are disabled in your simq configuration. Can not get stats for ' + file + '.'
file = window.require.resolve(file)
files[file] = mtime.getTime()
else
for file in dependencies[Cache.FILES]
file = path.resolve(file)
files[file] = (new Date(Cache.getFs().statSync(file).mtime)).getTime()
result[Cache.FILES] = files
return result
module.exports = Storage