Comments (5)
This was also the issue I was running into. I created a simplified version of the above code:
const initCache = () =>
new Promise((resolve, reject) => {
memoryCache = cacheManager.caching({
store: fsStore,
path: 'cache',
ttl: 12 * 60 * 60,
preventfill: false,
reviveBuffers: false,
fillcallback: data => {
resolve()
}
})
})
And I call that in my app like so
class MyClass {
constructor(){
initCache().then( //do some other stuff )
}
}
from node-cache-manager-fs.
I'm messing with your code a bit. The DiskStore.prototype.intializefill
seems to be finding the .dat file and finding the values and it sets it in this.collection
, but it still doesn't seem to work with the above code.
from node-cache-manager-fs.
The problem is a race condition. You are calling wrap before the cache is filled. You have to wait for the fillcallback before you can allow a call to wrap. The solution is to wrap cacheManager.caching in a promise that is resolved when the diskcache is filled. For example:
'use strict';
const rp = require('request-promise');
const cacheManager = require('cache-manager');
const fsStore = require('cache-manager-fs');
const _getFileNames = (opts) => {
return new Promise((resolve, reject) => {
const options = {
uri: opts.uri,
auth: opts.auth,
json: true,
timeout: 2 * 60 * 1000,
};
return rp(options)
.then((reponse) => {
return resolve(reponse);
})
.catch((error) => {
return reject(error);
});
});
};
const diskCachePromise = () => {
return new Promise((resolve) => {
const diskCache = cacheManager.caching(
{
store: fsStore,
options: {
ttl: 60 * 60 * 24 /* seconds */,
path: 'diskcache',
preventfill: false,
fillcallback: () => resolve(diskCache),
},
});
});
}
const diskCache = diskCachePromise();
const getFileNames = (opts) => {
return new Promise((resolve, reject) => {
diskCache.then((diskCache) => {
diskCache.wrap('getFileNames', () => {
return _getFileNames(opts);
})
.then((result) => {
return resolve(result);
})
.catch((error) => {
return reject(error);
});
});
});
};
module.exports = {
getFileNames,
};
from node-cache-manager-fs.
@rossvz 's post is helpful. I assume that only happens once data size reaches a certain size to lose the race condition. The second part can be written as await initCache();
too within an async function.
Another thing to know is that diskCache does not support mget()
. It will fail without an error message.
This was hard to diagnose because I set up a multiCache using a memoryCache (which worked) and a diskCache (which didn't). Switching my cache fetch from mget()
, to a loop using get()
allowed disk cache to work and was needed to get data pre-filling from disk working.
from node-cache-manager-fs.
the DiskCache is intended to run as a "singleton" - only one instance at one time and the api is running based on callbacks - no async, no promises... added that into the readme
from node-cache-manager-fs.
Related Issues (18)
- Can you release the latest version with reviveBuffers to NPM? HOT 2
- Does not delete the old files(Expired cache file) HOT 1
- How to clear file cache when ttl has expired HOT 1
- Module doesn't delete if ttl expired HOT 1
- We cannot use client side rendering HOT 1
- get method returning undefined HOT 3
- Library throws an exception when trying to recover cached values which cache files has been deleted HOT 4
- Support of larger file size HOT 1
- Missing parameter in get method HOT 3
- cache key as filename HOT 2
- Data is not cached
- Location of cache files? HOT 1
- ENOENT: no such file or directory on the odd occasion when cache retrieves up around 50 per second
- Delete operation does not support options arg HOT 2
- when caching a Buffer, the cache returns json HOT 1
- Breaking change introduced in 1.0.5 HOT 1
- Doesn't reinitialize with zip option enabled HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-cache-manager-fs.