Git Product home page Git Product logo

Comments (6)

adriancooney avatar adriancooney commented on May 8, 2024 1

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.

adriancooney avatar adriancooney commented on May 8, 2024

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.

xts-bit avatar xts-bit commented on May 8, 2024

@adriancooney Thanks Adrian, Any idea How to delete all the cache from kv?

from edge-config.

xts-bit avatar xts-bit commented on May 8, 2024

@adriancooney Is ttl is set like this? await kv.set(cacheKey, result, { ttl: 100 });

from edge-config.

LongDinhh avatar LongDinhh commented on May 8, 2024

@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.

scottroot avatar scottroot commented on May 8, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.