Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Constrain the cache size by removing the oldest caches

  • Loading branch information...
commit f1872f4640a043ee88f04b87161f0e0c4d2767e0 1 parent 9304d24
@smidwap smidwap authored
Showing with 14 additions and 8 deletions.
  1. +14 −8 lib/assets/javascripts/turbolinks.js.coffee
View
22 lib/assets/javascripts/turbolinks.js.coffee
@@ -55,21 +55,27 @@ fetchHistory = (cachedPage) ->
cacheCurrentPage = ->
pageCache[currentState.url] =
- body: document.body,
- title: document.title,
- positionY: window.pageYOffset,
- positionX: window.pageXOffset
+ body: document.body,
+ title: document.title,
+ positionY: window.pageYOffset,
+ positionX: window.pageXOffset,
+ cachedAt: new Date().getTime()
- #constrainPageCacheTo cacheSize
+ constrainPageCacheTo cacheSize
pagesCached = (size = cacheSize) ->
cacheSize = parseInt(size) if /^[\d]+$/.test size
constrainPageCacheTo = (limit) ->
- for own key, value of pageCache when key <= currentState.position - limit
+ pageCacheKeys = Object.keys pageCache
+
+ cacheTimesRecentFirst = pageCacheKeys.map (url) ->
+ pageCache[url].cachedAt
+ .sort (a, b) -> b - a
+
+ for key in pageCacheKeys when pageCache[key].cachedAt <= cacheTimesRecentFirst[limit]
triggerEvent 'page:expire', pageCache[key]
- pageCache[key] = null
- return
+ delete pageCache[key]
changePage = (title, body, csrfToken, runScripts) ->
document.title = title
Please sign in to comment.
Something went wrong with that request. Please try again.