diff --git a/lib/cache/cache.ex b/lib/cache/cache.ex
index 201360e..714f288 100644
--- a/lib/cache/cache.ex
+++ b/lib/cache/cache.ex
@@ -93,13 +93,18 @@ defmodule Cache.Registry do
# We have multiple clear_keys and need to update the state for it.
%{cache: cache, caches_by_key: caches_by_key} = state
+ # IO.inspect(clear_keys, label: "Clearing these keys")
+ # IO.inspect(cache, label: "Cache before clearing the keys")
+
cache =
Enum.reduce(clear_keys, cache, fn clear_key, cache ->
keys_to_remove = Map.get(caches_by_key, clear_key, [])
+ log_key_to_remove!(keys_to_remove)
cache = Map.drop(cache, keys_to_remove)
cache
end)
+ # IO.inspect(cache, label: "Cache after clearing the keys")
caches_by_key = Map.drop(caches_by_key, clear_keys)
%{state | cache: cache, caches_by_key: caches_by_key}
@@ -135,4 +140,29 @@ defmodule Cache.Registry do
%{state | cache: cache}
end
+
+ defp log_key_to_remove!(request_key_to_remove) do
+ # Write to the database when a cache for a url is cleared
+ Enum.map(request_key_to_remove, fn req_key ->
+ {method, path, query, auth} = req_key
+ uuid = Sparql.generate_uuid()
+ auth_escaped = Support.sparql_escape(auth)
+ query = """
+ PREFIX mu:
+ PREFIX mucache:
+ INSERT DATA {
+ GRAPH {
+ a mucache:CacheClear ;
+ mu:uuid "#{uuid}";
+ mucache:path "#{path}";
+ mucache:method "#{method}";
+ mucache:query "#{query}";
+ mucache:muAuthAllowedGroups \"\"\"#{auth_escaped}\"\"\";
+ mucache:muAuthUsedGroups \"\"\"#{auth_escaped}\"\"\".
+ }
+ }
+ """
+ sparql_request = Support.update(query)
+ end)
+ end
end