pwshub.com

Introducing Web Cache API support on Deno Deploy

Building a performant API, server, or function should not be complicated. Deno Deploy hosts your code close to your users around the world to minimize latency, and provides a simple KV cloud primitive that reads data from the closest data center, all with just a few clicks or keystrokes.

Today, we’re thrilled to give you even more control over performance with beta support for the Web Cache API in Deno Deploy. Much like the web standard Cache API available in modern browsers, this API offers semi-persistent storage for Request/Response pairs, enabling you to cache network requests and provide fast responses.

In this blog post, we’ll go over how to use the Cache API, what to expect from performance, some details on pricing, and more.

  • Using the Cache API
  • Cache Policy
  • Performance
  • Pricing
  • What’s next

🚨️ Host secure, performant JavaScript or TypeScript in the cloud in less than five minutes on Deno Deploy. Sign up for a free account today. 🚨️

Using the Cache API

In order to use the Cache API, you first open a cache with caches.open() method, which returns a Promise that resolves with the Cache object.

const cache = await caches.open("my-cache");

To retrieve from cache, you can use the cache.match() method, which accepts a Request object as a parameter.

const cachedResponse = await cache.match(request);

Finally, to add a new Request/Response object pair into cache, you can use cache.put():

await cache.put(req, res);

In practice, your web server with the new cache capabilities might look something like this:

const cache = await caches.open("my-cache");
Deno.serve(async (req) => {
  const cached = await cache.match(req);
  if (cached) {
    return cached;
  }
  const res = new Response("cached at " + new Date().toISOString());
  await cache.put(req, res.clone());
  return res;
});

Here’s a more sophisticated example that caches a call to an external API:

const cache = await caches.open("default");
Deno.serve(async (req: Request, ctx) => {
  const clientIp = ctx.remoteAddr.hostname;
  const cacheKey = `http://client-ip.local/${clientIp}`;
  const cached = await cache.match(cacheKey);
  if(cached) return cached;
  const res = await fetch(`https://api.country.is/${clientIp}`);
  if(!res.ok) return res;
  await cache.put(cacheKey, res.clone());
  return res;
});

Note that Cache is currently only supported for Deno Deploy and not Subhosting. For more information on using the Cache API, please refer to our documentation or to see all available Cache methods, please refer to our Cache reference documentation.

Cache Policy

By default, cached data is persisted for an indefinite period of time. While we periodically scan and delete inactive objects, an object is usually kept in cache for at least 30 days.

You can customize object expiration with standard HTTP headers Expires and Cache-Control:


const res = new Response("hello cache", {
  headers: {
    "Expires": new Date(Date.now() + 3600 * 1000).toUTCString(),
  },
});
await cache.put(req, res.clone());
const res = new Response("hello cache", {
  headers: {
    "Cache-Control": "max-age=3600",
  },
});
await cache.put(req, res.clone());

Performance

Under the hood, the Deno Deploy cache API is powered by a multi-tier log-structured merge-tree (LSM) storage engine. Cached data is managed as 512KB “pages”; depending on frequency of access, a page can reside in one or more locations among RAM, local NVMe SSD, or a cloud object store like S3 or GCS. The cache service issues I/O requests to the disk using io_uring, achieving 2.4Gbps throughput and sub-millisecond latency for random I/O workloads when running on one local SSD on GCP.

Pricing

During Web Cache API’s beta period, it will be available for all Deno Deploy users at no cost. While we hope to keep the Cache API free, we’ll closely monitor its usage and incurred costs throughout this beta period to determine if we need to start charging.

If you’re concerned about trying out the Web Cache API on Deno Deploy due to potential costs later, our measured cost of Cache is about 10x lower than that of Deno KV. In the event we end up charging for Web Cache, it will be in a similar ratio.

If pricing remains a concern or blocker for your project or preventing you from trying the Web Cache API, please get in touch. We’d love to learn more about your use case and find a solution that works for everyone.

What’s next

While we continue to improve the Web Cache API, it’s just one of many tools to improve performance. We’re also working to support HTTP caching on Deno Deploy, offering you even more fine grained control over how you serve content and overall performance.

🚨️ Deno 2 is right around the corner 🚨️

In a few days the first “Release Candidate” of Deno 2 will be released.

There are some minor breaking changes in Deno 2.

You’ll be able to get it using deno upgrade rc, but you can already future proof your code by adding the DENO_FUTURE=1 env var today.

Source: deno.com

Related stories
1 month ago - HELLO EVERYONE!!! It’s August 16th 2024 and you are reading the 24th edition of the Codeminer42’s tech news report. Let’s check out what the tech …
1 month ago - None of these sites need to be hostile to use. All of them would be significantly more useable if states abandoned the client-side-rendering approach, and along with it, the legacy JavaScript frameworks (React, Angular, etc.) built to...
2 weeks ago - WebAuthn on Chrome can now use hints, Related Origin Requests and JSON serialization
1 month ago - July is here, and we are back with a fresh set of resources for our fellow web developers. This month, we have a variety of tools and libraries covering different areas of web development, from libraries, frameworks, guides, and best...
2 weeks ago - Designing for digital products requires a different mindset than traditional websites. It’s all about continuous adaptation, refining, and iterating as user behavior and needs evolve. Paul Boag reflects on the key differences, including...
Other stories
2 hours ago - Ubuntu 24.10 ‘Oracular Oriole’ is released on October 13th, and as you’d expect from a new version of Ubuntu, it’s packed with new features. As a short-term release, Ubuntu 24.10 gets 9 months of ongoing updates, security patches, and...
3 hours ago - Did you know that CSS can play a significant role in web accessibility? While CSS primarily handles the visual presentation of a webpage, when you use it properly it can enhance the user’s experience and improve accessibility. In this...
5 hours ago - Design thinking workshops are your key to turning big problems into clear solutions. In this blog, I share how to run them efficiently and keep your team aligned. The post How to run a design thinking workshop appeared first on LogRocket...
5 hours ago - New memory-optimized X8g instances offer up to 3 TiB DDR5 memory, 192 vCPUs, and 50 Gbps network bandwidth, designed for memory-intensive workloads like databases, analytics, and caching with unparalleled price/performance and efficiency.
5 hours ago - Gain indispensable data engineering expertise through a hands-on specialization by DeepLearning.AI and AWS. This professional certificate covers ingestion, storage, querying, modeling, and more.