Skip to content

Instantly share code, notes, and snippets.

@kocisov
Created October 2, 2018 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kocisov/5600a653c0b4fe84b045c69d5a7e1fcf to your computer and use it in GitHub Desktop.
Save kocisov/5600a653c0b4fe84b045c69d5a7e1fcf to your computer and use it in GitHub Desktop.
// main process then
const { app } = require('electron')
const fs = require('fs')
const directory = app.getPath('home') // /home folder on OS X
const cacheDirectory = `${directory}/twitch-bot-cache` // /home/twitch-bot-cache/
const cacheDataFile = `${cacheDirectory}/data.json` // /home/twitch-bot-cache/data.json
// "cached now" object
let caches = {}
// if cacheDirectory isn't available, we create it
if (fs.existsSync(cacheDirectory) === false) {
fs.mkdirSync(cacheDirectory)
}
// fsCache object, contains functions, etc.
export const fsCache = {
key(name, value) {
// example: fsCache.key('button', 'is green'), fsCache.key('super_button', { is: 'green' })
caches[name] = value
// rewrites cache file with new data
fs.writeFileSync(cacheDataFile, JSON.stringify(caches, null, 2))
}
}
export function readFromCache(file, callback) {
try {
// read cache file
const cacheContent = fs.readFileSync(
`${cacheDirectory}/${file}.json`,
'utf-8'
)
// run callback with file content (cached data, etc.)
callback(cacheContent)
} catch (err) {
console.log('Error in Cache:', err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment