Comments (6)
If you want to remove all the keys from your database, you can use FLUSHDB
. Careful though, this will permanently delete all data in the cache!
await kv.flushdb()
from edge-config.
Hi @xts-bit, generally we try to use our Github issues as a place to track bugs and feature requests. For general help requests, you might get a response quicker in the Vercel Community Discussions or from the Vercel Support.
To answer your question, in your route handler, you would first check if the data your user is requesting is already in the cache via kv.get
. If it is and your happy the data is still fresh, you can return it from your handler via res.json
and skip hitting your database. If the data is not in the cache, you fetch it from the database, set it in the cache and then return it to the user via res.json
. You can optionally set a Time-to-live (TTL) on the data in your cache so it doesn't persist forever and you don't serve your users stale data indefinitely. The cache key for your data should be composed of the input you give to the handler so that you don't return the same response for different input (e.g. page
).
Your code might look something like this:
+import { kv } from "@vercel/kv";
router.get('/posts', async (req, res) => {
try {
const page = Number(req.query.page) || 1;
const limit = Number(req.query.limit) || 50;
+ const cacheKey = `posts-${page}-${limit}`;
+ const cachedValue = await kv.get(cacheKey);
+
+ if (cachedValue) {
+ return res.json(cachedValue);
+ }
const skip = (page - 1) * limit;
const result = await User.aggregate([
{ $project: { posts: 1 } },
{ $unwind: '$posts' },
{ $project: { postImage: '$posts.post', date: '$posts.date' } },
{ $sort: { date: -1 } },
{ $skip: skip },
{ $limit: limit },
]);
+ await kv.set(cacheKey, result);
res.json(result);
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Internal server error' });
}
});
Since we don't have any bug or feature request here, I'm going to close this out. Thanks for using KV.
from edge-config.
@adriancooney Thanks Adrian, Any idea How to delete all the cache from kv?
from edge-config.
@adriancooney Is ttl is set like this? await kv.set(cacheKey, result, { ttl: 100 });
from edge-config.
@adriancooney Is ttl is set like this?
await kv.set(cacheKey, result, { ttl: 100 });
await kv.set('setExample', '123abc', { ex: 100, nx: true });
Options
The SET command supports a set of options that modify its behavior:
- EX seconds -- Set the specified expire time, in seconds.
- PX milliseconds -- Set the specified expire time, in milliseconds.
- EXAT timestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds.
- PXAT timestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds.
- NX -- Only set the key if it does not already exist.
- XX -- Only set the key if it already exists.
- KEEPTTL -- Retain the time to live associated with the key.
- GET -- Return the old string stored at key, or nil if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.
from edge-config.
@adriancooney Is ttl is set like this?
await kv.set(cacheKey, result, { ttl: 100 });
await kv.set('setExample', '123abc', { ex: 100, nx: true });
Options
The SET command supports a set of options that modify its behavior:
- EX seconds -- Set the specified expire time, in seconds.
- PX milliseconds -- Set the specified expire time, in milliseconds.
- EXAT timestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds.
- PXAT timestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds.
- NX -- Only set the key if it does not already exist.
- XX -- Only set the key if it already exists.
- KEEPTTL -- Retain the time to live associated with the key.
- GET -- Return the old string stored at key, or nil if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.
Hello, is there no option like this for hset? I'd like to do what the op was talking about, setting TTL for the whole hash assigned to the key.
from edge-config.
Related Issues (20)
- Add the ability to get the latest updatedAt first in the https://vercel.com/docs/storage/vercel-blob/using-blob-sdk#list method HOT 2
- Cannot delete blob database HOT 2
- Is there a way to change timezone for a session HOT 1
- Vercel Blob: Access denied, please provide a valid token for this resource HOT 15
- Progress rate of upload to Blob HOT 2
- Support for Signed URLs in Blob HOT 2
- Getting a broken image file from Blob storage HOT 2
- How to get the url by pathname? HOT 4
- Kysely Migrations fail because VercelPostgresDialect fails to report that VercelPostgres does not support transactions HOT 2
- Cannot use @vercel/blob in Edge runtime HOT 4
- Intermittent Request Failures with Vercel Postgres and Next.js HOT 6
- Vercel KV Request vs Storage Limit
- insert query are cached HOT 5
- @vercel/kv Inconsistent increment behavior in Server Action HOT 5
- @vercel/postgres: Extended constant is not working HOT 5
- [vercel/blob] file should use the true mime type when uploading the file HOT 3
- Vercel KV not reachable HOT 2
- @vercel/postgres parameters bind error HOT 1
- Vercel Postgres Polling & Revalidation on Focus HOT 5
- X-Frame-Options set to DENY prevents embedding PDF blob in object, embed or iframe HOT 3
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 edge-config.