@ -23,6 +23,7 @@ exports.getCached = function(filename) {
cf . last _call = Date . now ( ) ; // set the last call to now
cf . last _call = Date . now ( ) ; // set the last call to now
cf . call _count += 1 ; // increase the call count
cf . call _count += 1 ; // increase the call count
if ( cf . data != null ) return cf . data ;
if ( cf . data != null ) return cf . data ;
logger . debug ( "Returning cached data for %s : %s" , filename , cf . path ) ;
return fs . readFileSync ( cf . path ) ; // return either the data or read the file
return fs . readFileSync ( cf . path ) ; // return either the data or read the file
} ;
} ;
@ -32,21 +33,38 @@ exports.getCached = function(filename) {
* @ param { String } data The data form the file
* @ param { String } data The data form the file
* /
* /
exports . cache = function ( filename , data ) {
exports . cache = function ( filename , data ) {
logger . verbose ( "Creating cache entry for %s" , filename ) ;
if ( ! fs . existsSync ( "./.cache" ) ) fs . mkdirSync ( "./.cache" ) ; // if the cache folder doesn't exist, create it
if ( ! fs . existsSync ( "./.cache" ) ) fs . mkdirSync ( "./.cache" ) ; // if the cache folder doesn't exist, create it
let cache _fn = filename . replace ( /[^\w\.]/g , "__" ) ; // remove invalid path characters
let cache _fn = filename . replace ( /[^\w\.]/g , "__" ) ; // remove invalid path characters
let count = 0 ;
let count = 0 ;
while ( fs . existsSync ( filename + count + ".cache" ) ) count ++ ; // check if a file with the same name already exists and increase count
while ( fs . existsSync ( filename + count + ".cache" ) ) count ++ ; // check if a file with the same name already exists and increase count
let cache _path = path . join ( cache _dir , cache _fn + count + ".cache" ) ; // create the final file path. Cachedir + cached filename (without invalid) + count + .cache
let cache _path = path . join ( cache _dir , cache _fn + count + ".cache" ) ; // create the final file path. Cachedir + cached filename (without invalid) + count + .cache
logger . debug ( "Creating file %s" , cache _path ) ;
fs . writeFile ( cache _path , data , ( error ) => {
fs . writeFile ( cache _path , data , ( error ) => {
logger . debug ( "Created file cache entry for %s" , filename ) ;
cache [ filename ] = { // create a cache-entry with the file's path when the file is written (so it won't be accessed before)
cache [ filename ] = { // create a cache-entry with the file's path when the file is written (so it won't be accessed before)
"path" : cache _path , // the last call to the file, the count of calls and an
"path" : cache _path , // the last call to the file, the count of calls and an
"last_call" : null , // empty data field to store the file's data if the file
"last_call" : null , // empty data field to store the file's data if the file
"call_count" : 0 , // was called often
"call_count" : 0 , // was called often
"data" : null ,
"data" : null ,
"creation_time" : Date . now ( )
"creation_time" : Date . now ( ) ,
"changed" : false
} ;
} ;
fs . watch ( filename , ( eventType ) => { // watch the file for changes
logger . debug ( "Change detected on %s" , filename ) ;
if ( eventType == 'change' ) cache [ filename ] . changed = true ; // if the event change is detected set the change attribute to true
} ) ;
} ) ; // write the data asynchronously to the file
} ) ; // write the data asynchronously to the file
} ;
} ;
var logger = require ( "winston" ) ;
/ * *
* Sets the logger for logging
* @ param { Winston Logger } newLogger
* /
exports . setLogger = function ( newLogger ) {
logger = newLogger ;
}
/ * *
/ * *
* Returns if the file is already cached
* Returns if the file is already cached
@ -59,18 +77,23 @@ exports.cache = function(filename, data) {
* /
* /
exports . isCached = function ( filename ) {
exports . isCached = function ( filename ) {
let cached _entry = cache [ filename ] ;
let cached _entry = cache [ filename ] ;
if ( cached _entry ) {
if ( cached _entry ) { // check if the cache entry exists
if ( cached _entry . path ) {
logger . debug ( "Found cache entry for %s" , filename ) ;
return fs . existsSync ( cached _entry . path ) ;
if ( cached _entry . changed ) return false ; // if a change was detected recache the file
if ( cached _entry . path ) { // check if the path variable is set
logger . debug ( "Found path entry for %s" , filename )
return fs . existsSync ( cached _entry . path ) ; // return if the file exists
}
}
}
}
return false ;
logger . debug ( "Found no cache entry for %s" , filename ) ;
return false ; // return false if the cache entry doesn't exist
}
}
/ * *
/ * *
* A function that dumps the config into the config file after appending the cache to it .
* A function that dumps the config into the config file after appending the cache to it .
* /
* /
exports . cleanup = function ( ) {
exports . cleanup = function ( ) {
logger . verbose ( "Dumping cache into cache_dump file" ) ;
cache _dump [ "last" ] = cache ;
cache _dump [ "last" ] = cache ;
fs . writeFileSync ( config _path , JSON . stringify ( cache _dump ) ) ;
fs . writeFileSync ( config _path , JSON . stringify ( cache _dump ) ) ;
}
}