Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chia-datalayer-kv-cache

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chia-datalayer-kv-cache - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

90

index.js

@@ -7,2 +7,3 @@ const NodeCache = require("node-cache");

const { getChiaRoot } = require("chia-root-resolver");
const defaultConfig = require("./defaultConfig");

@@ -20,6 +21,9 @@ let config = defaultConfig;

const getValue = async (storeId, key) => {
const getValue = async ({ id: storeId, key }) => {
// Try to get the value from cache
const cacheKey = `${storeId}-${key}`;
let value = memoryCache.get(cacheKey);
if (value) {
console.log(`Served ${storeId} from memory cache`);
}

@@ -32,3 +36,4 @@ if (value === undefined) {

try {
value = await fs.readFile(filePath, "utf8");
value = JSON.parse(await fs.readFile(filePath, "utf8"));
console.log(`Served ${storeId} from file cache`);
memoryCache.set(cacheKey, value); // Cache the file content

@@ -40,3 +45,3 @@ } catch (error) {

value = await datalayer.getValue(storeId, key); // Assuming the datalayer has a getValue method
value = await datalayer.getValue({ id: storeId, key });

@@ -46,3 +51,3 @@ memoryCache.set(cacheKey, value);

fs.mkdir(path.dirname(filePath), { recursive: true })
.then(() => fs.writeFile(filePath, JSON.stringify(data), "utf8"))
.then(() => fs.writeFile(filePath, JSON.stringify(value), "utf8"))
.catch((err) => {

@@ -52,3 +57,6 @@ console.error(err);

} else {
throw error; // Rethrow the error if it's not 'ENOENT'
return {
success: false,
error: error,
};
}

@@ -61,17 +69,72 @@ }

const getKeys = async ({ id: storeId }) => {
// Try to get the value from cache
const cacheKey = storeId;
let value = memoryCache.get(cacheKey);
if (value) {
console.log(`Served ${storeId} from memory cache`);
}
if (value === undefined) {
// If not in cache, try to get the value from the file system
const cacheDirectory = getCacheDirectory();
const filePath = path.join(cacheDirectory, storeId, storeId);
try {
value = JSON.parse(await fs.readFile(filePath, "utf8"));
console.log(`Served ${storeId} from file cache`);
memoryCache.set(cacheKey, value); // Cache the file content
} catch (error) {
if (error.code === "ENOENT") {
// If the file does not exist, get the data from the datalayer
const datalayer = Datalayer.rpc(config);
value = await datalayer.getKeys({ id: storeId });
memoryCache.set(cacheKey, value);
fs.mkdir(path.dirname(filePath), { recursive: true })
.then(() => fs.writeFile(filePath, JSON.stringify(value), "utf8"))
.catch((err) => {
console.error(err);
});
} else {
return {
success: false,
error: error,
};
}
}
}
return value;
};
const invalidateCache = (storeId, key) => {
// invalidate node-cache
const cacheKey = `${storeId}-${key}`;
memoryCache.del(cacheKey);
let cacheKey;
let filePath;
// delete the file if it exists
const cacheDirectory = getCacheDirectory();
const filePath = path.join(cacheDirectory, storeId, key);
if (key) {
cacheKey = `${storeId}-${key}`;
filePath = path.join(getCacheDirectory(), storeId, key);
} else {
cacheKey = storeId;
filePath = path.join(getCacheDirectory(), storeId);
}
memoryCache.keys().forEach((storedKey) => {
if (storedKey.startsWith(cacheKey)) {
memoryCache.del(storedKey);
}
});
// delete the file or directory if it exists
fs.access(filePath, fs.constants.F_OK, (err) => {
if (!err) {
// file exists, delete it
fs.unlink(filePath, (err) => {
// file or directory exists, delete it
fs.rm(filePath, { recursive: true, force: true }, (err) => {
if (err) {
console.error(`Error while deleting the file: ${err}`);
console.error(`Error while deleting the file or directory: ${err}`);
}

@@ -86,3 +149,4 @@ });

getValue,
getKeys,
invalidateCache,
};

5

package.json
{
"name": "chia-datalayer-kv-cache",
"version": "1.0.0",
"version": "1.0.1",
"description": "Return Cached Key/Value pairs when requested from Chia DataLayer",

@@ -24,4 +24,5 @@ "main": "index.js",

"chia-datalayer-wrapper": "^1.0.3",
"chia-root-resolver": "^1.0.0"
"chia-root-resolver": "^1.0.0",
"node-cache": "^5.1.2"
}
}
# chia-datalayer-kv-cache
A caching module for Chia's datalayer key-value stores. It caches the values of keys in a store, in both memory (using the node-cache package) and on the file system. The package checks for cached values in the memory cache and file system before finally fetching from the datalayer if not found.
`chia-datalayer-kv-cache` is an NPM package designed to interact with Chia's data layer and provide a caching mechanism to enhance performance. It primarily offers methods to get and invalidate cached values, either from memory cache or from a filesystem cache.
The module provides an additional layer of caching for datalayer requests, enhancing speed and efficiency for frequent data retrievals. It leverages both in-memory caching for swift access to recent data and file system caching for longer-term, persistent storage.
The package uses the NodeCache library for in-memory caching, and filesystem caching is achieved by storing values in a designated cache directory on disk. The package also includes support for Chia's data layer with the help of the `chia-datalayer-wrapper` package.
## Installation
```
```bash
npm install chia-datalayer-kv-cache

@@ -15,9 +15,11 @@ ```

Before using the `getValue` function, you may optionally use the `configure` function to specify the hosts and paths for your Chia configuration. If not used, the module will default to certain predefined values.
### Promise Chain
```javascript
const { configure, getValue } = require('chia-datalayer-kv-cache');
const {
configure,
getValue,
getKeys,
invalidateCache,
} = require("chia-datalayer-kv-cache");
// Optional: Configure the module
configure({

@@ -31,32 +33,53 @@ full_node_host: "https://localhost:8555",

getValue('storeId', 'key')
.then(value => console.log(value))
.catch(error => console.error(error));
// Get a value from cache or datalayer
getValue({ id: "storeId", key: "key" })
.then((value) => {
console.log(value);
})
.catch((error) => {
console.error(error);
});
// Get all keys for a storeId from cache or datalayer
getKeys({ id: "storeId" })
.then((value) => {
console.log(value);
})
.catch((error) => {
console.error(error);
});
// Invalidate a cache entry
invalidateCache("storeId", "key");
```
### Async/Await
Using async/await:
```javascript
const { configure, getValue } = require('chia-datalayer-kv-cache');
const {
configure,
getValue,
getKeys,
invalidateCache,
} = require("chia-datalayer-kv-cache");
configure({
full_node_host: "https://localhost:8555",
datalayer_host: "https://localhost:8562",
wallet_host: "https://localhost:9256",
certificate_folder_path: "~/.chia/mainnet/config/ssl",
default_wallet_id: 1,
});
async function main() {
configure({
// your configuration here...
});
async function main() {
try {
const value = await getValue('storeId', 'key');
console.log(value);
} catch (error) {
console.error(error);
}
const value = await getValue({ id: "storeId", key: "key" });
console.log(value);
const keys = await getKeys({ id: "storeId" });
console.log(keys);
invalidateCache("storeId", "key");
}
main();
main().catch((error) => console.error(error));
```
If no configuration is provided using `configure()`, the module uses default values which can be found in the `defaultConfig.js` file.
## API

@@ -66,25 +89,16 @@

Configures the module with the given `newConfig`. This step is optional, and if not performed, default configuration values will be used.
Merge the provided configuration object with the current configuration.
- `newConfig` - A configuration object containing properties:
- `full_node_host`
- `datalayer_host`
- `wallet_host`
- `certificate_folder_path`
- `default_wallet_id`
### `getValue({ id: storeId, key })`
### `getValue(storeId, key)`
Fetches the value associated with the provided `storeId` and `key` from the cache. If it's not present in the cache, it fetches from the Chia's data layer, storing the value in the cache for future retrieval.
Attempts to get the value of a key in a store. First checks the memory cache, then the file system cache, and finally the datalayer if the key is not found in the caches. Returns a promise that resolves to the value of the key.
### `getKeys({ id: storeId })`
- `storeId` - The ID of the store.
- `key` - The key to get the value of.
Fetches all keys for the given `storeId` from the cache. If they're not present in the cache, it fetches from the Chia's data layer, storing the keys in the cache for future retrieval.
### `invalidateCache(storeId, key)`
Invalidates the cache for a specific key in a store, both in the memory cache and the file system cache.
Invalidates a cache entry. If only the `storeId` is provided, it invalidates all entries related to that store. If both `storeId` and `key` are provided, it invalidates the specific cache entry.
- `storeId` - The ID of the store.
- `key` - The key to invalidate the cache of.
## Support

@@ -91,0 +105,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc