Verified Commit 2ae3bdee authored by Camil Staps's avatar Camil Staps 🚀

Move pruning cache to separate container

parent e5467e20
...@@ -2,7 +2,6 @@ implementation module Cache ...@@ -2,7 +2,6 @@ implementation module Cache
import StdFunc import StdFunc
import StdTuple import StdTuple
import StdOrdList
from Data.Func import $ from Data.Func import $
import Control.Monad import Control.Monad
import Control.Applicative import Control.Applicative
...@@ -11,14 +10,11 @@ import Crypto.Hash.MD5 ...@@ -11,14 +10,11 @@ import Crypto.Hash.MD5
import Text.JSON import Text.JSON
import Data.Error import Data.Error
import StdFile import StdFile
import System.Directory
import System.FilePath import System.FilePath
import System.File import System.File
import System.Time
import Data.Tuple import Data.Tuple
CACHE_DIR :== "./cache" CACHE_DIR :== "./cache"
CACHE_SIZE :== 100
toCacheFile :: (a -> FilePath) | toString a toCacheFile :: (a -> FilePath) | toString a
toCacheFile = ((</>) CACHE_DIR) o md5 o toString toCacheFile = ((</>) CACHE_DIR) o md5 o toString
...@@ -27,26 +23,4 @@ readCache :: !a -> *World -> (Maybe b, !*World) | toString a & JSONDecode{|*|} b ...@@ -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) 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 :: !a !b -> *World -> *World | toString a & JSONEncode{|*|} b
writeCache k v = purgeCache o snd o writeFile (toCacheFile k) (toString $ toJSON v) writeCache k v = 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]
...@@ -40,3 +40,9 @@ services: ...@@ -40,3 +40,9 @@ services:
MYSQL_DATABASE: cloogledb MYSQL_DATABASE: cloogledb
MYSQL_USER: cloogle MYSQL_USER: cloogle
MYSQL_PASSWORD: cloogle MYSQL_PASSWORD: cloogle
gc:
build: gc
volumes:
- "./cache:/var/cache"
restart: always
FROM bash
COPY gc.sh /
CMD ["bash", "/gc.sh"]
#!/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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment