Skip to content

Commit

Permalink
Add cache clear events to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-PieterBaert committed Jul 18, 2021
1 parent 90d14ed commit 61f26c8
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/cache/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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 = Support.generate_uuid()
auth_escaped = Support.sparql_escape(auth)
query = """
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX mucache: <http://mu.semte.ch/vocabularies/cache/>
INSERT DATA {
GRAPH<http://mu.semte.ch/application> {
<http://semte.baert.jp.net/cache-clear/v0.1/#{uuid}> 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

0 comments on commit 61f26c8

Please sign in to comment.