cache-storage
Version:
[ABANDONED] Advanced cache storage for node js
89 lines (63 loc) • 1.57 kB
text/coffeescript
Storage = require './Storage'
Cache = require '../../Cache'
fs = null
path = null
class FileStorage extends Storage
directory: null
allData: null
data: null
meta: null
constructor: () ->
if typeof window != 'undefined'
throw new Error 'FileStorage: Can not use this storage in browser'
fs = require 'fs'
path = require 'path'
= path.resolve()
if !Cache.getFs().existsSync()
throw new Error 'FileStorage: directory ' + + ' does not exists'
if !Cache.getFs().statSync().isDirectory()
throw new Error 'FileStorage: path ' + + ' must be directory'
getFileName: ->
return + '/__' + .namespace + '.json'
loadData: (fn) ->
if == null
file = ()
Cache.getFs().exists(file, (exists) =>
if exists
Cache.getFs().readFile(file, encoding: 'utf8', (err, data) =>
if err
fn(err, null)
else
= JSON.parse(data)
fn(null, )
)
else
= {data: {}, meta: {}}
fn(null, )
)
else
fn(null, )
getData: (fn) ->
( (err, data) ->
fn(err, data.data)
)
getMeta: (fn) ->
( (err, data) ->
fn(err, data.meta)
)
writeData: (, , fn) ->
=
data:
meta:
file = ()
Cache.getFs().writeFile(file, JSON.stringify(
data:
meta:
), (err) ->
if err
fn(err)
else
fn(null)
)
return @
module.exports = FileStorage