diff options
author | Ben Morrison <ben@gbmor.dev> | 2020-05-07 17:05:22 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2020-05-07 17:05:22 -0400 |
commit | e8970e6aca23b457243688c795844ee85c73f846 (patch) | |
tree | 2ae529dd088b683ae43efb546d94bf76eb9bc4e3 /cache.go | |
parent | 693efbb4a96f6f4ea195a63d563f355d03206de1 (diff) | |
download | api-e8970e6aca23b457243688c795844ee85c73f846.tar.gz |
index page is caching
Diffstat (limited to 'cache.go')
-rw-r--r-- | cache.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..88d5dba --- /dev/null +++ b/cache.go @@ -0,0 +1,58 @@ +package main + +import ( + "io/ioutil" + "log" + "sync" + "time" +) + +// Holds the cached responses +type page struct { + raw []byte + expires time.Time +} + +// Wraps the page cache map with a rwlock +type cacheWrapper struct { + sync.RWMutex + pages map[string]*page +} + +// The actual page/response cache +var cache = &cacheWrapper{ + pages: make(map[string]*page), +} + +func bapCache(requestPath string) { + cache.RLock() + if cache.pages[requestPath] == nil { + cache.RUnlock() + cacheIndex() + return + } + + expires := cache.pages[requestPath].expires + cache.RUnlock() + + if time.Now().After(expires) { + cacheIndex() + } +} + +// Pulls the index page from disk and places it into the cache. +// etag is an fnv32 hash of the raw file bytes, truncated if necessary. +func cacheIndex() { + bytes, err := ioutil.ReadFile("web/index.txt") + if err != nil { + log.Printf("Could not read index page: %s", err.Error()) + bytes = []byte("tilde.institute informational API") + } + + cache.Lock() + cache.pages["/"] = &page{ + raw: bytes, + expires: time.Now().Add(5 * time.Minute), + } + cache.Unlock() +} |