Skip to content

Commit

Permalink
Merge pull request #3545 from cloudflare/mar/memory-cache-delete
Browse files Browse the repository at this point in the history
Implement delete operation for memory cache.
  • Loading branch information
mar-cf authored Feb 21, 2025
2 parents ce57483 + b40aae2 commit f7bcdbb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ wd_cc_library(
],
visibility = ["//visibility:public"],
deps = [
"//src/workerd/io:compatibility-date_capnp",
"//src/workerd/jsg",
"//src/workerd/util:uuid",
],
Expand Down
15 changes: 15 additions & 0 deletions src/workerd/api/memory-cache.c++
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ void SharedMemoryCache::Use::handleFallbackFailure(InProgress& inProgress) const
}
}

void SharedMemoryCache::Use::delete_(const kj::String& key) const {
auto data = cache->data.lockExclusive();
cache->removeIfExistsWhileLocked(*data, key);
}

// Attempts to serialize a JavaScript value. If that fails, this function throws
// a tunneled exception, see jsg::createTunneledException().
static kj::Own<CacheValue> hackySerialize(jsg::Lock& js, jsg::JsRef<jsg::JsValue>& value) {
Expand Down Expand Up @@ -444,6 +449,16 @@ jsg::Promise<jsg::JsRef<jsg::JsValue>> MemoryCache::read(jsg::Lock& js,
}
}

void MemoryCache::delete_(jsg::Lock& js, jsg::NonCoercible<kj::String> key) {
// Ignore operations on keys exceeding key max size.
if (key.value.size() > MAX_KEY_SIZE) {
js.throwException(js.rangeError("Key too large."_kj));
return;
}

cacheUse.delete_(key.value);
}

// ======================================================================================

MemoryCacheProvider::MemoryCacheProvider(const kj::MonotonicClock& timer,
Expand Down
11 changes: 10 additions & 1 deletion src/workerd/api/memory-cache.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <workerd/io/compatibility-date.capnp.h>
#include <workerd/jsg/jsg.h>
#include <workerd/util/uuid.h>

Expand Down Expand Up @@ -201,6 +202,8 @@ class SharedMemoryCache: public kj::AtomicRefcounted {
kj::OneOf<kj::Own<CacheValue>, kj::Promise<GetWithFallbackOutcome>> getWithFallback(
const kj::String& key, SpanBuilder& span) const;

void delete_(const kj::String& key) const;

private:
// Creates a new FallbackDoneCallback associated with the given
// InProgress struct. This is called whenever getWithFallback() wants to
Expand Down Expand Up @@ -482,8 +485,14 @@ class MemoryCache: public jsg::Object {
jsg::NonCoercible<kj::String> key,
jsg::Optional<FallbackFunction> optionalFallback);

JSG_RESOURCE_TYPE(MemoryCache) {
// Delete a value from the cache.
void delete_(jsg::Lock& js, jsg::NonCoercible<kj::String> key);

JSG_RESOURCE_TYPE(MemoryCache, CompatibilityFlags::Reader flags) {
JSG_METHOD(read);
if (flags.getMemoryCacheDelete()) {
JSG_METHOD_NAMED(delete, delete_);
}
}

private:
Expand Down
5 changes: 5 additions & 0 deletions src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,9 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
$experimental
$neededByFl;
# Enables cache settings specified request in fetch api cf object to override cache rules. (only for user owned or grey-clouded sites)

memoryCacheDelete @73 :Bool
$compatEnableFlag("memory_cache_delete")
$experimental;
# Enables delete operations on memory cache if enabled.
}

0 comments on commit f7bcdbb

Please sign in to comment.