jaredwray / cacheable

a robust, scalable, and maintained set of caching packages
https://cacheable.org
MIT License
1.64k stars 166 forks source link

Primary store of Cacheable always serializes data #895

Closed rskvazh closed 2 weeks ago

rskvazh commented 2 weeks ago

Describe the bug I cannot store objects with BigInt values. I think this is overhead if I would use serialize (superjson, devalue...) for in-memory cache. Primary store of Cacheable serializes data.

I tried new Cacheable({ primary: new KeyvCacheableMemory() }), but it not helps because it wraps KeyvCacheableMemory with Keyv.

How To Reproduce (best to provide workable code or tests!)

const primary = new KeyvCacheableMemory()
const m = new Cacheable({ primary })
console.log(await m.set('key1', { a: new Date(), b: BigInt(123) }))
// false
rskvazh commented 2 weeks ago

I think CacheableMemory is great for my task and working ok, but stats unavailable for it.

jaredwray commented 2 weeks ago

@rskvazh - thanks so much for this post and just recently we added the ability to bypass the serialization (great use case for in-memory caching). I have not fully tested this but this should work if you are using the latest Keyv v5.2.1

import Keyv from 'keyv';
import {KeyvCacheableMemory, Cacheable} from 'cacheable';

const primary = new Keyv({ store: new KeyvCacheableMemory });
primary.serialize = undefined;
primary.deserialize = undefined;

const cache = new Cacheable({ primary });

Again, I have not tried it out yet but I believe this would work. If not, the older way is to use your own custom serialization on Keyv that supports BigInt. You can read about it here: https://keyv.org/docs/keyv/#custom-serializers

rskvazh commented 2 weeks ago

Thank you! 🙏 Will try it soon.

jaredwray commented 2 weeks ago

Let me know how it goes and I’m happy to figure out how to help!

rskvazh commented 2 weeks ago

Yeap, its worked! Thank you.