question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Utilities] Create caching utility

See original GitHub issue

We need a helper function that will get/set/delete files from a cache directory.

On a local machine that would be the .netlify/cache dir

Inside Netlify CI that directory is path.join('/opt/build/cache')

The caching utilities should be smart and save file hashes. A v1 attempt at this was done over here: https://github.com/DavidWells/cache-me-outside

These utilities will be used by plugin authors to make it easier for them to interact with the file cache.

File cache might be used for file storage or for storing values between builds. E.g. the google lighthouse plugin storing page scores between builds

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
DavidWellscommented, Oct 8, 2019

Thinking something like this. save (or set), get, delete (or remove)

// Simple save values to cache
const cacheData = cache.save(path, contents)

// Save values to cache
const cacheData = cache.save(path, contents, {
  // time to live
  expires: '14 days'
})

// Simple get cache, no checks for invalid or not
const filePaths = cache.get(path)

// Get cached file or directory
const filePaths = cache.get(path, {
  shouldCacheUpdate: (cacheData) => {
    /* any kind of logic here
      - check date cache last updated
      - Make remote api request and diff a value
      - Diff dependencies files like package.json
      - Whatever you want
    */

    // Return true if cache needs to be updated. False if cache is good to use
    return true
  },
  handleCacheUpdate: () => {
    // exec command / custom logic to refresh the cache
  }
})

// Delete
cache.delete(path)

The cacheData being referred to above is something like:

{
  "createdOn": 1534296638475,
  "createdOnDate": "Wed, 15 Aug 2018 01:30:38 GMT",
  "modifiedOn": 1534300695541,
  "cacheDir": "/Users/davidwells/Netlify/cache-magic/cache/Netlify/cache-magic/serverless-test",
  "cacheDirContents": "/Users/davidwells/Netlify/cache-magic/cache/Netlify/cache-magic/serverless-test/.serverless",
  "contents": {
    "src": "/Users/davidwells/Netlify/cache-magic/serverless-test/.serverless",
    "hash": "0496d16c0a8b1d43ca2d3c77ca48a8e237fdb625",
    "files": {
      "stuff.txt": "11b80f260a5eea9e867a23ab7f96aff77080ff90"
    }
  }
}

This information is available for the shouldCacheUpdate function to quickly diff hashes. That data is also returned from the save method

See how should update works in this flow chart.

I wasn’t too crazy about cache-me-outside’s API taking an array of these config objects. The updating (shouldUpdate + handleUpdate) logic works really well though.

There is also a diff util that is available in here. This is very handy for not reimplementing hashing logic.

Example:

cache.get('path', {
  handleCacheUpdate: 'npm install && echo "this runs when cache is invalid"',
  shouldCacheUpdate: async (cacheManifest, utils) => {
    const packageJson = path.join(__dirname, 'package.json')
    const packageJsonChanged = await utils.diff(packageJson)
    return packageJsonChanged
  }
})
0reactions
ehmickycommented, Jan 16, 2020

README improved in #704.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create Cache Schema utility—ArcGIS Server
The Create Cache Schema utility allows you to define a map or image service cache from the command line, including the scales, server...
Read more >
Caching Server Utility on the Mac App Store - Apple
Caching Server Utility is a macOS app to discover what caching servers available on the network. Makes troubleshooting easier.
Read more >
-Xshareclasses option - IBM
Some cache utilities can work with caches from previous Java™ versions or caches that are created by JVMs with different bit-widths.
Read more >
Delete Cache utility—ArcGIS Server
The Delete Cache utility allows you to delete map or image service caches using the command line. This utility deletes the entire cache,...
Read more >
Create Cache Schema utility - ArcGIS Server
The Create Cache Schema utility allows you to define a map or image service cache from the command line, including the scales, server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found