AlbinoDrought / cachios

Simple axios cache wrapper using node-cache
MIT License
88 stars 10 forks source link

Keyv cache #61

Closed Zyles closed 3 years ago

Zyles commented 3 years ago

So I had to drop LRU cache because the saved JSON blob got corrupted @ 14000 items...

And decided to try keyv instead with SQLite3 adapter. Which seems like a stronger option with potential real database support. It seems to support get/set like cachios expects but it does not seem to be as plug as play as I thought.

What am I missing here?

npm install --save keyv
npm install --save @keyv/sqlite
import * as cachiosInstance from 'cachios'
import axios from 'axios'
import Keyv from 'keyv'

const api = axios.create({
  baseURL: 'https://example.com/',
  headers: {
    'X-User-Agent': 'API Client',
    Accept: 'application/json',
    'Content-Type': 'application/json'
  }
})

const cachios = cachiosInstance.create(api)

cachios.cache = new Keyv('sqlite://cache.sqlite', { namespace: 'test' })

cachios.cache.cacheCount = async function () {
  const cacheSize = await cachios.cache.opts.store.query('SELECT COUNT(*) as count FROM keyv')
  return cacheSize[0].count
}

async function main () {

  // This works
  await cachios.cache.set('foo', 'never expires')
  await cachios.cache.set('foo2', 'expires in 1 second', 1000)
  console.log(await cachios.cache.get('foo'))

  console.log('CacheSize is', await cachios.cache.cacheCount()) // Returns 2

  // This does not work
  await cachios.get('/cachetest') // not cached
      .then(async () => await cachios.get('/cachetest')) // cached
      .then(async () => {
        console.log('Cache test contains:', cachios.cache.itemCount) // 1 item in cache - the first request

        console.log('CacheSize is', await cachios.cache.cacheCount()) // Still returns 2
      })

}

main()

Output:

Successfully compiled 12 files with Babel (1657ms).
never expires
CacheSize is 2
Cache test contains: undefined
CacheSize is 2

SQLite DB also remains empty so it is obviously not writing there.

Do I need to write custom readCache and writeCache functions for this?

Thanks.

AlbinoDrought commented 3 years ago

The getCachedValue and setCachedValue methods in cachios are synchronous and do not currently support async/await or Promises. Changing this would need to be done around here: https://github.com/AlbinoDrought/cachios/blob/master/src/cachios.js#L55

Zyles commented 3 years ago

Gotcha... I changed everything to async and it seems to work.

So I assume you will not add that option to this library?

AlbinoDrought commented 3 years ago

I'll try and check it out on the weekend. If added, I'll need to use Promises instead of async/await due to the current project setup - Cachios supports both Node and browser SPAs but doesn't use a bundler or build process.

I previously stripped the bundler from Cachios around October 2018 in #43. I try to keep the dependencies up to date and wasn't a fan of these frequent updates:

image

(as a side effect, this package can be published with just an npm publish and the code in this repo matches the code in the package repository 1:1)

I'll post here if implemented. I don't think it will be a breaking change. In the meantime, forking Cachios or just copying the repo to your project and modifying it should work :+1:

AlbinoDrought commented 3 years ago

:+1: Released as v3.0.0 and added as an example to the repo: https://github.com/AlbinoDrought/cachios/tree/master/examples/persistent-cache-sqlite-keyv