Skip to content

Instantly share code, notes, and snippets.

@kocisov
Created October 2, 2018 18:13
Show Gist options
  • Save kocisov/5600a653c0b4fe84b045c69d5a7e1fcf to your computer and use it in GitHub Desktop.
Save kocisov/5600a653c0b4fe84b045c69d5a7e1fcf to your computer and use it in GitHub Desktop.

Revisions

  1. kocisov created this gist Oct 2, 2018.
    40 changes: 40 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    // 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)
    }
    }