Skip to content

Commit 61f26c8

Browse files
Add cache clear events to the database
1 parent 90d14ed commit 61f26c8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/cache/cache.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,18 @@ defmodule Cache.Registry do
9393
# We have multiple clear_keys and need to update the state for it.
9494
%{cache: cache, caches_by_key: caches_by_key} = state
9595

96+
# IO.inspect(clear_keys, label: "Clearing these keys")
97+
# IO.inspect(cache, label: "Cache before clearing the keys")
98+
9699
cache =
97100
Enum.reduce(clear_keys, cache, fn clear_key, cache ->
98101
keys_to_remove = Map.get(caches_by_key, clear_key, [])
102+
log_key_to_remove!(keys_to_remove)
99103
cache = Map.drop(cache, keys_to_remove)
100104
cache
101105
end)
102106

107+
# IO.inspect(cache, label: "Cache after clearing the keys")
103108
caches_by_key = Map.drop(caches_by_key, clear_keys)
104109

105110
%{state | cache: cache, caches_by_key: caches_by_key}
@@ -135,4 +140,29 @@ defmodule Cache.Registry do
135140

136141
%{state | cache: cache}
137142
end
143+
144+
defp log_key_to_remove!(request_key_to_remove) do
145+
# Write to the database when a cache for a url is cleared
146+
Enum.map(request_key_to_remove, fn req_key ->
147+
{method, path, query, auth} = req_key
148+
uuid = Support.generate_uuid()
149+
auth_escaped = Support.sparql_escape(auth)
150+
query = """
151+
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
152+
PREFIX mucache: <http://mu.semte.ch/vocabularies/cache/>
153+
INSERT DATA {
154+
GRAPH<http://mu.semte.ch/application> {
155+
<http://semte.baert.jp.net/cache-clear/v0.1/#{uuid}> a mucache:CacheClear ;
156+
mu:uuid "#{uuid}";
157+
mucache:path "#{path}";
158+
mucache:method "#{method}";
159+
mucache:query "#{query}";
160+
mucache:muAuthAllowedGroups \"\"\"#{auth_escaped}\"\"\";
161+
mucache:muAuthUsedGroups \"\"\"#{auth_escaped}\"\"\".
162+
}
163+
}
164+
"""
165+
sparql_request = Support.update(query)
166+
end)
167+
end
138168
end

0 commit comments

Comments
 (0)