Skip to content

Commit

Permalink
Bug 1211984 - PR fixes, added dynamic cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Leroux committed Nov 2, 2015
1 parent b24153a commit 7d760a1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
16 changes: 8 additions & 8 deletions Client/Frontend/Home/TopSitesPanel.swift
Expand Up @@ -82,7 +82,9 @@ class TopSitesPanel: UIViewController {
make.edges.equalTo(self.view)
}
self.collection = collection
self.refreshHistory(maxFrecencyLimit)

self.profile.history.setTopSitesCacheSize(Int32(maxFrecencyLimit))
self.refreshTopSites(maxFrecencyLimit)
}

deinit {
Expand All @@ -92,7 +94,7 @@ class TopSitesPanel: UIViewController {
func notificationReceived(notification: NSNotification) {
switch notification.name {
case NotificationFirefoxAccountChanged:
refreshHistory(maxFrecencyLimit)
refreshTopSites(maxFrecencyLimit)
break
default:
// no need to do anything at all
Expand Down Expand Up @@ -135,15 +137,13 @@ class TopSitesPanel: UIViewController {
}
}

private func refreshHistory(frequencyLimit: Int) {
private func refreshTopSites(frequencyLimit: Int) {

This comment has been minimized.

Copy link
@rnewman

rnewman Nov 2, 2015

Contributor

s/frequency//

// Reload right away with whatever is in the cache, then check to see if the cache is invalid. If it's invalid,
// invalidate the cache and requery. This allows us to always show results right away if they are cached but
// also load in the up-to-date results asynchronously if needed
reloadTopSitesWithLimit(frequencyLimit) >>== {
self.profile.history.invalidateTopSitesIfNeeded().upon { result in
if result {
self.reloadTopSitesWithLimit(frequencyLimit)
}
reloadTopSitesWithLimit(frequencyLimit) >>> {
return self.profile.history.invalidateTopSitesIfNeeded() >>== { result in
return result ? self.reloadTopSitesWithLimit(frequencyLimit) : succeed()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Providers/Profile.swift
Expand Up @@ -234,6 +234,7 @@ public class BrowserProfile: Profile {
}
}

// These selectors run on which ever thread sent the notifications (not the main thread)
@objc
func onProfileDidFinishSyncing(notification: NSNotification) {
history.setTopSitesNeedsInvalidation()
Expand Down
3 changes: 2 additions & 1 deletion Storage/History.swift
Expand Up @@ -29,7 +29,8 @@ public protocol BrowserHistory {

func getTopSitesWithLimit(limit: Int) -> Deferred<Maybe<Cursor<Site>>>
func setTopSitesNeedsInvalidation()
func invalidateTopSitesIfNeeded() -> Deferred<Bool>
func invalidateTopSitesIfNeeded() -> Deferred<Maybe<Bool>>
func setTopSitesCacheSize(size: Int32)
}

/**
Expand Down
29 changes: 16 additions & 13 deletions Storage/SQL/SQLiteHistory.swift
Expand Up @@ -8,8 +8,6 @@ import XCGLogger

private let log = Logger.syncLogger

private let TopSitesCacheSize = 100

class NoSuchRecordError: MaybeErrorType {
let guid: GUID
init(guid: GUID) {
Expand Down Expand Up @@ -268,18 +266,23 @@ extension SQLiteHistory: BrowserHistory {
prefs.setBool(false, forKey: PrefsKeys.KeyTopSitesCacheIsValid)
}

public func invalidateTopSitesIfNeeded() -> Deferred<Bool> {
let deferred = Deferred<Bool>()
if !(prefs.boolForKey(PrefsKeys.KeyTopSitesCacheIsValid) ?? false) {
purgeTopSitesCache() >>== {
self.updateTopSitesCacheWithLimit(TopSitesCacheSize) >>== {
deferred.fill(true)
}
}
} else {
deferred.fill(false)
public func invalidateTopSitesIfNeeded() -> Deferred<Maybe<Bool>> {
if prefs.boolForKey(PrefsKeys.KeyTopSitesCacheIsValid) ?? false {
return deferMaybe(false)
}

let cacheSize = Int(prefs.intForKey(PrefsKeys.KeyTopSitesCacheSize) ?? 0)
return purgeTopSitesCache()
>>> { self.updateTopSitesCacheWithLimit(cacheSize) }
>>> always(true)
}

public func setTopSitesCacheSize(size: Int32) {
let oldValue = prefs.intForKey(PrefsKeys.KeyTopSitesCacheSize) ?? 0
if oldValue != size {
prefs.setInt(size, forKey: PrefsKeys.KeyTopSitesCacheSize)
setTopSitesNeedsInvalidation()
}
return deferred
}

private func updateTopSitesCacheWithLimit(limit : Int) -> Success {
Expand Down
1 change: 1 addition & 0 deletions Utils/Prefs.swift
Expand Up @@ -8,6 +8,7 @@ public struct PrefsKeys {
public static let KeyLastRemoteTabSyncTime = "lastRemoteTabSyncTime"
public static let KeyLastSyncFinishTime = "lastSyncFinishTime"
public static let KeyTopSitesCacheIsValid = "topSitesCacheIsValid"
public static let KeyTopSitesCacheSize = "topSitesCacheSize"
}

public protocol Prefs {
Expand Down

0 comments on commit 7d760a1

Please sign in to comment.