`rmmbr` — Super Simple Persistent Caching
Here’s a familiar scenario: you’re working on a project that involves making requests to a costly API like OpenAI, which can quickly rack up your bill if you’re not careful. You might be tempted to cache the results to avoid making the same expensive requests multiple times, but implementing a caching solution which is persistent across runs can be time-consuming and complex. This is where rmmbr
comes in.
rmmbr
is a lightweight service with client libraries in Javascript, TypeScript or Python. It provides a decorator you can place around your asynchronous functions to cache their results, either locally or in the cloud. With just a few lines of code, you can significantly reduce the number of API requests you make, saving you time and money.
To use rmmbr
, simply install it via npm (or pip):
npm i rmmbr
Let’s take a look at an example using OpenAI’s GPT-3 API:
import { localCache } from "rmmbr";
import openai from "openai";
const cacher = localCache({ id: "createChatCompletion" });
const callOpenAI = (params) =>
openai.createChatCompletion(params).then(({ data }) => data);
const cachedOpenAI = cacher(callOpenAI);
const doPrompt = (prompt) =>
cachedOpenAI({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
}).then(({ choices }: OpenAIOutput) => choices[0].message?.content || "");
In this example, we’re using the localCache
function to cache the result of an asynchronous function that generates text using OpenAI’s GPT-3 API. The cacher stores the requests in a file in a local directory under the id
name.
The doPrompt
function takes a prompt as an argument and uses the OpenAI API to generate text based on that prompt. However, instead of making a new API request every time the function is called, we're using localCache
to cache the result of the API request. This means that if the function is called again with the same prompt, it will return the cached result instead of making a new API request.
If you want, you can also store the results in the cloud with e2e encryption.
const cacher = cloudCache({
token: "service-token",
url: "https://rmmbr.net",
// Time to live in seconds, can omit this if undesired.
ttl: 123,
// e2ee for sensiive data, can omit this if undesired.
encryptionKey: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
});
The backend is open source as well, so you can use your own instance if you’d like.
We’d love to hear about your use cases and desired features (e.g. encryption? ttl?), so feel free to post them as issues here.
Overall, rmmbr
is a powerful yet simple library that can help you save time and money by caching the results of expensive API requests. Whether you need to cache data locally or persist it across devices, rmmbr
has you covered. Give it a try in your next project and see how it can help you optimize your code!
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Interested in scaling your software startup? Check out Circuit.