diff --git a/backend/Cache.icl b/backend/Cache.icl index 677735d7b9d3dc9b124c292a71b3afa731a56607..cb13efacd3bdcd21e5812aa9bf46552c74bc6741 100644 --- a/backend/Cache.icl +++ b/backend/Cache.icl @@ -2,7 +2,6 @@ implementation module Cache import StdFunc import StdTuple -import StdOrdList from Data.Func import $ import Control.Monad import Control.Applicative @@ -11,14 +10,11 @@ import Crypto.Hash.MD5 import Text.JSON import Data.Error import StdFile -import System.Directory import System.FilePath import System.File -import System.Time import Data.Tuple CACHE_DIR :== "./cache" -CACHE_SIZE :== 100 toCacheFile :: (a -> FilePath) | toString a toCacheFile = (() CACHE_DIR) o md5 o toString @@ -27,26 +23,4 @@ readCache :: !a -> *World -> (Maybe b, !*World) | toString a & JSONDecode{|*|} b readCache k = appFst (join o fmap (fromJSON o fromString) o error2mb) o readFile (toCacheFile k) writeCache :: !a !b -> *World -> *World | toString a & JSONEncode{|*|} b -writeCache k v = purgeCache o snd o writeFile (toCacheFile k) (toString $ toJSON v) - -purgeCache :: *World -> *World -purgeCache w -# (fs,w) = readDirectory CACHE_DIR w -| isError fs - = w -# fs = map (() CACHE_DIR) $ fromOk fs -| length fs < CACHE_SIZE - = w -# (infos,w) = seqList (map (\f st -> let (i,w) = getFileInfo f st in ((f,i),w)) fs) w -| any (isError o snd) infos - = w -# infos = map (appSnd fromOk) infos -# infos = sortBy (\(_,a) (_,b) -> usedLess a b) infos -= snd $ deleteFile (fst $ hd infos) w -where - usedLess :: FileInfo FileInfo -> Bool - usedLess a b = a.lastAccessedTime < b.lastAccessedTime - -instance < Tm -where - (<) a b = [a.year,a.yday,a.hour,a.min,a.sec] < [b.year,b.yday,b.hour,b.min,b.sec] +writeCache k v = snd o writeFile (toCacheFile k) (toString $ toJSON v) diff --git a/docker-compose.yml b/docker-compose.yml index 3b3b34c18bce4841c41a66162588fb77bf3c7d71..82a614748a23ec85bb6d0126362691278abe7e7f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,3 +40,9 @@ services: MYSQL_DATABASE: cloogledb MYSQL_USER: cloogle MYSQL_PASSWORD: cloogle + + gc: + build: gc + volumes: + - "./cache:/var/cache" + restart: always diff --git a/gc/Dockerfile b/gc/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..6fb0abdeef4aaf121d7355b106613a24a32cafaa --- /dev/null +++ b/gc/Dockerfile @@ -0,0 +1,5 @@ +FROM bash + +COPY gc.sh / + +CMD ["bash", "/gc.sh"] diff --git a/gc/gc.sh b/gc/gc.sh new file mode 100755 index 0000000000000000000000000000000000000000..9de3475d6e5d6163030dc09d24e817919c0c3393 --- /dev/null +++ b/gc/gc.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +CACHE_SIZE=1000 +INTERVAL=600 +CACHE_DIR=/var/cache + +cd "$CACHE_DIR" + +while :; do + n="$(($(ls -l | wc -l)-1))" + if [ "$n" -gt "$CACHE_SIZE" ]; then + ls -1tu | tail -n "$((n-CACHE_SIZE))" | xargs -n1 rm -v + else + echo "$n / $CACHE_SIZE cache entries used" + fi + sleep $INTERVAL +done