Skip to content

Commit

Permalink
Update cache mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinFechner committed Oct 12, 2020
1 parent b105fef commit 1c8cb17
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 47 deletions.
15 changes: 14 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [5.2 Examples](#52-examples)
- [5.3 Result](#53-result)
- [JSON output: `/api/search/<search-id>?q=<query>`](#json-output-apisearchsearch-idqquery)
- [6. Caching](#6-caching)

## 1. APPCONF

Expand Down Expand Up @@ -252,4 +253,16 @@ Search for a text in manuscipt descriptions:
Results in:

```json
```
```

## 6. Caching

Due to performance some of the API calls are cached. So calls like `/api/<object-type>`
result in a cache for all entities of `<object-type>` and their properties.

If data is changed and the cache can be rebuild by adding a GET parameter
to the above API calls:

- `cache`. Possible values are:
- `no` cache is rebuild if newer data exists
- `reset` cache is always rebuild (exception: cache is not rebuild if it is newer than 1 minute)
8 changes: 4 additions & 4 deletions build.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#Fri, 25 Sep 2020 18:18:04 +0200
#Wed, 07 Oct 2020 15:54:23 +0200
project.version.major=1
project.version.minor=9
project.version.patch=4
project.version.patch=6

project.app.name=ediarum.web
project.app.folder=.

repository.version=v1.1.1-28-gf46ddf8
repository.version=v1.9.4-1-gb105fef

project.version.date=2020-09-25.18-18-04
project.version.date=2020-10-07.15-54-23
Binary file not shown.
68 changes: 42 additions & 26 deletions content/edweb-api.xql
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,31 @@ declare function edwebapi:filter-list(
declare function edwebapi:get-all(
$app-target as xs:string,
$limit as xs:string?,
$cache as xs:string
$cache as xs:string,
$with-filter as xs:boolean
)
{
let $object-types := edwebapi:get-config($app-target)//appconf:object/@xml:id
let $found-objects :=
for $object-type in $object-types
let $map :=
edwebapi:load-map-from-cache(
"edwebapi:get-object-list",
[$app-target, $object-type, $limit],
if ($cache = "yes")
then ()
else edwebapi:data-collection($app-target),
$cache = "no"
)
if ($with-filter)
then
edwebapi:load-map-from-cache(
"edwebapi:get-object-list",
[$app-target, $object-type, $limit],
edwebapi:data-collection($app-target),
$cache = "no",
$cache = "reset"
)
else
edwebapi:load-map-from-cache(
"edwebapi:get-object-list-without-filter",
[$app-target, $object-type, $limit],
edwebapi:data-collection($app-target),
$cache = "no",
$cache = "reset"
)
return $map?list
return
map:merge((map:entry("date-time", current-dateTime()), $found-objects))
Expand Down Expand Up @@ -211,7 +221,8 @@ declare function edwebapi:load-map-from-cache(
$function-name as xs:string,
$params as array(*),
$data-collection as xs:string?,
$reload as xs:boolean?
$soft-reload as xs:boolean?,
$hard-reload as xs:boolean?
) as map(*)
{
let $node-set as node()* :=
Expand All @@ -233,27 +244,28 @@ declare function edwebapi:load-map-from-cache(
let $cache-file-name := substring-after($function-name, ":")||"-"
||translate(string-join($params?*[.!='cache'], "-"),'/','__')||".json"
let $load-cache := util:binary-doc($cache-collection||"/"||$cache-file-name)
let $load-from-cache := exists($load-cache)
let $cache-exists := exists($load-cache)
let $load-map :=
if($load-from-cache)
if($cache-exists)
then parse-json(util:binary-to-string($load-cache))
else false()
let $current-date-time := current-dateTime()
let $cache-is-new :=
if ($load-from-cache)
if ($cache-exists)
then ($current-date-time < xs:dateTime($load-map?date-time) + xs:dayTimeDuration("PT1M"))
else false()
let $load-from-cache := $load-from-cache and not($reload)
let $cache-is-up-to-date :=
if ($load-from-cache and count($node-set)=0)
if ($cache-exists and count($node-set)=0)
then true()
else if ($load-from-cache)
else if ($cache-exists and $soft-reload)
then
let $since := $load-map?date-time
let $last-modified := xmldb:find-last-modified-since($node-set, $since)
return count($last-modified)=0
else if ($cache-exists)
then true()
else false()
let $load-from-cache := ($load-from-cache and $cache-is-up-to-date) or $cache-is-new
let $load-from-cache := $cache-is-new or (not($hard-reload) and $cache-is-up-to-date)
let $apply-and-store :=
if ($load-from-cache)
then ()
Expand Down Expand Up @@ -470,6 +482,7 @@ declare function edwebapi:eval-filters-for-object(
$object-xml as node()
) as map(*)
{
let $cache := ""
let $filters := $object-def/appconf:filters/appconf:filter
let $data-collection := edwebapi:data-collection($app-target)
let $filter-values :=
Expand All @@ -493,7 +506,8 @@ declare function edwebapi:eval-filters-for-object(
"edwebapi:get-relation-list",
[$app-target, $rel-type-name, ()],
$data-collection,
false()
$cache = "no",
$cache = "reset"
)
let $items := $relations?list?*[?($rel-perspective) = $object?id]
for $i in $items return
Expand Down Expand Up @@ -856,10 +870,9 @@ declare function edwebapi:get-search-results(
edwebapi:load-map-from-cache(
"edwebapi:get-object-list",
[$app-target, $object-type, $limit],
if ($cache = "yes")
then ()
else edwebapi:data-collection($app-target),
$cache = "no"
edwebapi:data-collection($app-target),
$cache = "no",
$cache = "reset"
)
return
edwebapi:get-object-list-with-search(
Expand Down Expand Up @@ -985,6 +998,7 @@ declare function edwebapi:get-relation-list(
$limit as xs:string?
) as map(*)
{
let $cache := ""
let $config := edwebapi:get-config($app-target)
let $relation-type := $config//appconf:relation[@xml:id=$relation-type-name]
let $subject-type := $relation-type/@subject/string()
Expand All @@ -1005,8 +1019,9 @@ declare function edwebapi:get-relation-list(
edwebapi:load-map-from-cache(
"edwebapi:get-object-list-without-filter",
[$app-target, $object-type, $limit],
(),
false()
edwebapi:data-collection($app-target),
$cache = "no",
$cache = "reset"
)
return $map?list?* (: "list-without-filter" :)

Expand All @@ -1015,8 +1030,9 @@ declare function edwebapi:get-relation-list(
edwebapi:load-map-from-cache(
"edwebapi:get-object-list-without-filter",
[$app-target, $subject-type, $limit],
(),
false()
edwebapi:data-collection($app-target),
$cache = "no",
$cache = "reset"
)
return $map?list?* (: "list-without-filter" :)
let $data-collection := edwebapi:data-collection($app-target)
Expand Down
2 changes: 1 addition & 1 deletion expath-pkg.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://expath.org/ns/pkg" name="http://www.bbaw.de/telota/software/ediarum/web/lib" abbrev="edweb" version="1.9.4" spec="1.0">
<package xmlns="http://expath.org/ns/pkg" name="http://www.bbaw.de/telota/software/ediarum/web/lib" abbrev="edweb" version="1.9.6" spec="1.0">
<title>ediarum.web</title>
<xquery>
<namespace>http://www.bbaw.de/telota/software/ediarum/web/lib</namespace>
Expand Down
7 changes: 4 additions & 3 deletions views/api/all-list.xql
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ let $id-type :=
else $id-type

(: let $id-type := request:get-parameter("id-type", request:get-attribute("id-type")) :)
let $all-list := edwebapi:get-all($app-target, $limit, $cache)
return
if ($id-type = "all")
then
$all-list
let $all-list := edwebapi:get-all($app-target, $limit, $cache, false())
return $all-list
else
map:remove($all-list, "date-time")?*[?filter?($id-type)||"" != ""]
let $all-list := edwebapi:get-all($app-target, $limit, $cache, true())
return map:remove($all-list, "date-time")?*[?filter?($id-type)||"" != ""]
21 changes: 9 additions & 12 deletions views/api/object-list.xql
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,27 @@ let $result :=
edwebapi:load-map-from-cache(
"edwebapi:get-object-list-without-filter",
[$app-target, $object-type, $limit],
if ($cache = "yes")
then ()
else edwebapi:data-collection($app-target),
$cache = "no"
edwebapi:data-collection($app-target),
$cache = "no",
$cache = "reset"
)
else if ($is-object)
then
edwebapi:load-map-from-cache(
"edwebapi:get-object-list",
[$app-target, $object-type, $limit],
if ($cache = "yes")
then ()
else edwebapi:data-collection($app-target),
$cache = "no"
edwebapi:data-collection($app-target),
$cache = "no",
$cache = "reset"
)
else if ($is-relation)
then
edwebapi:load-map-from-cache(
"edwebapi:get-relation-list",
[$app-target, $object-type, $limit],
if ($cache = "yes")
then ()
else edwebapi:data-collection($app-target),
$cache = "no"
edwebapi:data-collection($app-target),
$cache = "no",
$cache = "reset"
)
else $object-type||" isn't defined for '"||$app-target||"'."
return
Expand Down

0 comments on commit 1c8cb17

Please sign in to comment.