Fixed caching.
This commit is contained in:
parent
3af3de2ea3
commit
630831f772
2 changed files with 45 additions and 10 deletions
34
cache.nim
Normal file
34
cache.nim
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import tables, uri
|
||||
type
|
||||
CacheInfo = object
|
||||
valid: bool
|
||||
value: string
|
||||
|
||||
CacheHolder = ref object
|
||||
caches: Table[string, CacheInfo]
|
||||
|
||||
proc normalizePath(x: string): string =
|
||||
let u = parseUri(x)
|
||||
result = u.path & (if u.query != "": '?' & u.query else: "")
|
||||
|
||||
proc newCacheHolder*(): CacheHolder =
|
||||
new result
|
||||
result.caches = initTable[string, CacheInfo]()
|
||||
|
||||
proc invalidate*(cache: CacheHolder, name: string) =
|
||||
cache.caches.mget(name.normalizePath()).valid = false
|
||||
|
||||
proc invalidateAll*(cache: CacheHolder) =
|
||||
for key, val in mpairs(cache.caches):
|
||||
val.valid = false
|
||||
|
||||
template get*(cache: CacheHolder, name: string, grabValue: expr): expr =
|
||||
## Check to see if the cache contains value for ``name``. If it does and the
|
||||
## cache is valid then doesn't recalculate it but returns the cached version.
|
||||
echo(cache.caches)
|
||||
mixin normalizePath
|
||||
let nName = name.normalizePath()
|
||||
if not (cache.caches.hasKey(nName) and cache.caches[nName].valid):
|
||||
echo "Resetting cache."
|
||||
cache.caches[nName] = CacheInfo(valid: true, value: grabValue)
|
||||
cache.caches[nName].value
|
||||
21
forum.nim
21
forum.nim
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
import
|
||||
os, strutils, times, md5, strtabs, cgi, math, db_sqlite, matchers,
|
||||
rst, rstgen, captchas, scgi, jester, asyncdispatch, asyncnet
|
||||
rst, rstgen, captchas, scgi, jester, asyncdispatch, asyncnet, cache
|
||||
from htmlgen import tr, th, td, span
|
||||
|
||||
const
|
||||
|
|
@ -651,21 +651,18 @@ template createTFD(): stmt =
|
|||
if request.cookies.len > 0:
|
||||
checkLoggedIn(c)
|
||||
|
||||
var cached = ""
|
||||
|
||||
#var settings = newSettings()
|
||||
#settings.port = Port(5000)
|
||||
var cacheHolder = newCacheHolder()
|
||||
|
||||
routes:
|
||||
get "/":
|
||||
createTFD()
|
||||
c.isThreadsList = true
|
||||
var count = 0
|
||||
discard genThreadsList(c, count)
|
||||
if cached == "":
|
||||
cached = genMain(c, genThreadsList(c, count),
|
||||
additionalHeaders = genRSSHeaders(c), showRssLinks = true)
|
||||
resp cached
|
||||
let threadList = genThreadsList(c, count)
|
||||
let data = cacheHolder.get("/",
|
||||
genMain(c, threadList,
|
||||
additionalHeaders = genRSSHeaders(c), showRssLinks = true))
|
||||
resp data
|
||||
|
||||
get "/threadActivity.xml":
|
||||
createTFD()
|
||||
|
|
@ -744,6 +741,7 @@ routes:
|
|||
get "/logout/?":
|
||||
createTFD()
|
||||
logout(c)
|
||||
cacheHolder.invalidateAll()
|
||||
redirect(uri("/"))
|
||||
|
||||
get "/register/?":
|
||||
|
|
@ -772,6 +770,7 @@ routes:
|
|||
createTFD()
|
||||
if login(c, @"name", @"password"):
|
||||
finishLogin()
|
||||
cacheHolder.invalidateAll()
|
||||
else:
|
||||
resp c.genMain(genFormLogin(c))
|
||||
|
||||
|
|
@ -780,12 +779,14 @@ routes:
|
|||
if c.register(@"name", @"new_password", @"antibot", @"email"):
|
||||
discard c.login(@"name", @"new_password")
|
||||
finishLogin()
|
||||
cacheHolder.invalidateAll()
|
||||
else:
|
||||
resp c.genMain(genFormRegister(c))
|
||||
|
||||
post "/donewthread":
|
||||
createTFD()
|
||||
if newThread(c):
|
||||
cacheHolder.invalidate("/")
|
||||
redirect(uri("/"))
|
||||
else:
|
||||
body = ""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue