Added two different approaches to caching

1. Generates md5 for all source files
2. Creates an index of last modified times for all files
parent d92b0de0
...@@ -35,6 +35,7 @@ class App ...@@ -35,6 +35,7 @@ class App
contentDirectory: null contentDirectory: null
dataDirectory: null dataDirectory: null
logsDirectory: null logsDirectory: null
cacheDirectory: null
contentIndex: null contentIndex: null
trackerDomainToTrackerDetailsIndex: null trackerDomainToTrackerDetailsIndex: null
...@@ -72,6 +73,8 @@ class App ...@@ -72,6 +73,8 @@ class App
@logsDirectory = path.join @privateDirectory, 'logs' @logsDirectory = path.join @privateDirectory, 'logs'
@cacheDirectory = path.join @privateDirectory, 'cache'
# Set up logging. # Set up logging.
logFile = path.join @logsDirectory, 'App.log' logFile = path.join @logsDirectory, 'App.log'
logger = new winston.Logger logger = new winston.Logger
......
#
# Builder cache
#
fs = require 'fs-extra-as-promised'
path = require 'path-extra'
Promise = require 'thrush'
crypto = require 'crypto'
glob = require 'glob'
globAsync = Promise.promisify glob
# app = require './App'
class Cache
homeDirectory: null
contentDirectory: null
themesDirectory: null
cacheCurrentChecksumsDirectory: null
constructor: ->
@homeDirectory = path.join path.homedir(), 'better.fyi'
@contentDirectory = path.join @homeDirectory, 'content'
@themesDirectory = path.join @homeDirectory, 'themes'
@cacheCurrentChecksumsDirectory = path.join @homeDirectory, '.private', 'cache', 'current'
startTime = new Date
@cacheContent @contentDirectory
.then =>
@cacheContent @themesDirectory
.then =>
duration = (new Date) - startTime
console.log "Took: #{duration} ms"
cacheContent: (directory) =>
(globAsync "#{directory}/**/*", {nodir: true})
.series (file) =>
fileComponents = file.split '/'
filePathFragment = fileComponents[4..(fileComponents.length-2)].join '/'
fileName = fileComponents[(fileComponents.length - 1)]
cacheFilePath = path.join @cacheCurrentChecksumsDirectory, filePathFragment
cacheFile = path.join cacheFilePath, fileName
fs.readFileAsync file, 'utf-8'
.then (content) =>
hash = checksum content
# console.log "#{hash} : #{cacheFilePath}"
fs.ensureDirAsync cacheFilePath
.then =>
fs.outputFileAsync cacheFile, hash
#
# Calculate checksum
#
checksum = (str) ->
return crypto
.createHash('md5')
.update(str, 'utf8')
.digest('hex')
new Cache
#
# Builder cache using last modified times
#
fs = require 'fs-extra-as-promised'
path = require 'path-extra'
Promise = require 'thrush'
glob = require 'glob'
globAsync = Promise.promisify glob
# app = require './App'
class Cache
homeDirectory: null
contentDirectory: null
themesDirectory: null
cacheCurrentChecksumsDirectory: null
constructor: ->
@homeDirectory = path.join path.homedir(), 'better.fyi'
@contentDirectory = path.join @homeDirectory, 'content'
@themesDirectory = path.join @homeDirectory, 'themes'
@cacheCurrentModifiedTimesFile = path.join @homeDirectory, '.private', 'cache', 'current-modified-times.json'
startTime = new Date
@cacheContent @contentDirectory
.then =>
@cacheContent @themesDirectory
.then =>
duration = (new Date) - startTime
console.log "Took: #{duration} ms"
cacheContent: (directory) =>
lastModifiedTimes = {}
(globAsync "#{directory}/**/*", {nodir: true})
.series (file) =>
fileComponents = file.split '/'
filePathFragment = fileComponents[4..].join '/'
fs.statAsync file
.then (stats) =>
lastModifiedTimes[filePathFragment] = stats.mtime
.then =>
fs.outputFileAsync @cacheCurrentModifiedTimesFile, JSON.stringify lastModifiedTimes
new Cache
...@@ -343,6 +343,16 @@ configureLocalEnvironment(){ ...@@ -343,6 +343,16 @@ configureLocalEnvironment(){
if ! [ -d $logsFolder ]; then if ! [ -d $logsFolder ]; then
mkdir $logsFolder mkdir $logsFolder
fi fi
#
# Create the cache directory, if it doesn’t already exist.
#
readonly cacheFolder=$privateFolder/cache
if ! [ -d $cacheFolder ]; then
mkdir $cacheFolder
fi
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment