Fjalapeno has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/225481

Change subject: Implement Memory cache for articles
......................................................................

Implement Memory cache for articles

T105819

Adding NSCache to the datastore, setting cache size to 50
Note: NSCache is thread safe and locks itself.
Caching when an article is saved
Checking memory cache, then, disk, then create if needed
Moved from article fetched notification -> article saved notification (more 
like core data)

Change-Id: I0042dbf2bc7097cd3b4698216178cbe59ac4e30e
---
M MediaWikiKit/MediaWikiKit/MWKDataStore.h
M MediaWikiKit/MediaWikiKit/MWKDataStore.m
M Wikipedia/UI-V5/WMFArticleFetcher.h
M Wikipedia/UI-V5/WMFArticleFetcher.m
M Wikipedia/UI-V5/WMFArticleViewController.m
5 files changed, 35 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/ios/wikipedia 
refs/changes/81/225481/1

diff --git a/MediaWikiKit/MediaWikiKit/MWKDataStore.h 
b/MediaWikiKit/MediaWikiKit/MWKDataStore.h
index 84f9645..ea2c610 100644
--- a/MediaWikiKit/MediaWikiKit/MWKDataStore.h
+++ b/MediaWikiKit/MediaWikiKit/MWKDataStore.h
@@ -22,6 +22,9 @@
  */
 extern NSString* MWKCreateImageURLWithPath(NSString* path);
 
+extern NSString* const MWKArticleSavedNotification;
+extern NSString* const MWKArticleKey;
+
 @interface MWKDataStore : NSObject
 
 @property (readonly, copy, nonatomic) NSString* basePath;
diff --git a/MediaWikiKit/MediaWikiKit/MWKDataStore.m 
b/MediaWikiKit/MediaWikiKit/MWKDataStore.m
index 3ba71b0..72139db 100644
--- a/MediaWikiKit/MediaWikiKit/MWKDataStore.m
+++ b/MediaWikiKit/MediaWikiKit/MWKDataStore.m
@@ -7,8 +7,10 @@
 
 #import <BlocksKit/BlocksKit.h>
 
-
+NSString* const MWKArticleSavedNotification      = 
@"MWKArticleSavedNotification";
+NSString* const MWKArticleKey                    = @"MWKArticleKey";
 NSString* const MWKDataStoreValidImageSitePrefix = @"//upload.wikimedia.org/";
+
 NSString* MWKCreateImageURLWithPath(NSString* path) {
     return [MWKDataStoreValidImageSitePrefix stringByAppendingString:path];
 }
@@ -18,6 +20,7 @@
 @interface MWKDataStore ()
 
 @property (readwrite, copy, nonatomic) NSString* basePath;
+@property (nonatomic, strong) NSCache* articleCache;
 
 @end
 
@@ -36,7 +39,9 @@
 - (instancetype)initWithBasePath:(NSString*)basePath {
     self = [super init];
     if (self) {
-        self.basePath = basePath;
+        self.basePath                = basePath;
+        self.articleCache            = [[NSCache alloc] init];
+        self.articleCache.countLimit = 50;
     }
     return self;
 }
@@ -193,9 +198,14 @@
 }
 
 - (void)saveArticle:(MWKArticle*)article {
+    if (article.title.text == nil) {
+        return;
+    }
     NSString* path       = [self pathForArticle:article];
     NSDictionary* export = [article dataExport];
     [self saveDictionary:export path:path name:@"Article.plist"];
+    [self.articleCache setObject:article forKey:article.title];
+    [[NSNotificationCenter defaultCenter] 
postNotificationName:MWKArticleSavedNotification object:self 
userInfo:@{MWKArticleKey: article}];
 }
 
 - (void)saveSection:(MWKSection*)section {
@@ -266,6 +276,10 @@
 
 #pragma mark - load methods
 
+- (MWKArticle*)memoryCachedArticleWithTitle:(MWKTitle*)title {
+    return [self.articleCache objectForKey:title];
+}
+
 - (MWKArticle*)existingArticleWithTitle:(MWKTitle*)title {
     NSString* path     = [self pathForTitle:title];
     NSString* filePath = [path 
stringByAppendingPathComponent:@"Article.plist"];
@@ -274,7 +288,19 @@
 }
 
 - (MWKArticle*)articleWithTitle:(MWKTitle*)title {
-    return [self existingArticleWithTitle:title] ? : [[MWKArticle alloc] 
initWithTitle:title dataStore:self];
+    MWKArticle* article = [self memoryCachedArticleWithTitle:title];
+
+    if (!article) {
+        article = [self existingArticleWithTitle:title];
+        if (!article) {
+            article = [[MWKArticle alloc] initWithTitle:title dataStore:self];
+        }
+        if (article) {
+            [self.articleCache setObject:article forKey:article.title];
+        }
+    }
+
+    return article;
 }
 
 - (MWKSection*)sectionWithId:(NSUInteger)sectionId 
article:(MWKArticle*)article {
diff --git a/Wikipedia/UI-V5/WMFArticleFetcher.h 
b/Wikipedia/UI-V5/WMFArticleFetcher.h
index bdce398..72b3ead 100644
--- a/Wikipedia/UI-V5/WMFArticleFetcher.h
+++ b/Wikipedia/UI-V5/WMFArticleFetcher.h
@@ -7,12 +7,6 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-extern NSString* const WMFArticleFetchedNotification;
-extern NSString* const WMFArticleFetchedKey;
-
-extern NSString* const WMFArticleFetchedNotification;
-extern NSString* const WMFArticleFetchedKey;
-
 @interface WMFArticleFetcher : NSObject
 
 @property (nonatomic, strong, readonly) MWKDataStore* dataStore;
diff --git a/Wikipedia/UI-V5/WMFArticleFetcher.m 
b/Wikipedia/UI-V5/WMFArticleFetcher.m
index e5c23c0..94faf33 100644
--- a/Wikipedia/UI-V5/WMFArticleFetcher.m
+++ b/Wikipedia/UI-V5/WMFArticleFetcher.m
@@ -7,8 +7,6 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-NSString* const WMFArticleFetchedNotification = 
@"WMFArticleFetchedNotification";
-NSString* const WMFArticleFetchedKey          = @"WMFArticleFetchedKey";
 
 @interface WMFArticleFetcher ()
 
@@ -48,7 +46,6 @@
                 }
             });
         } completionBlock:^(MWKArticle* article) {
-            [[NSNotificationCenter defaultCenter] 
postNotificationName:WMFArticleFetchedNotification object:self 
userInfo:@{WMFArticleFetchedKey: article}];
             resolve(article);
         } errorBlock:^(NSError* error) {
             resolve(error);
diff --git a/Wikipedia/UI-V5/WMFArticleViewController.m 
b/Wikipedia/UI-V5/WMFArticleViewController.m
index e3e5c81..c36d555 100644
--- a/Wikipedia/UI-V5/WMFArticleViewController.m
+++ b/Wikipedia/UI-V5/WMFArticleViewController.m
@@ -115,15 +115,15 @@
 #pragma mark - Article Notifications
 
 - (void)observeArticleUpdates {
-    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(articleUpdatedWithNotification:) 
name:WMFArticleFetchedNotification object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(articleUpdatedWithNotification:) 
name:MWKArticleSavedNotification object:nil];
 }
 
 - (void)unobserveArticleUpdates {
-    [[NSNotificationCenter defaultCenter] removeObserver:self 
name:WMFArticleFetchedNotification object:nil];
+    [[NSNotificationCenter defaultCenter] removeObserver:self 
name:MWKArticleSavedNotification object:nil];
 }
 
 - (void)articleUpdatedWithNotification:(NSNotification*)note {
-    MWKArticle* article = note.userInfo[WMFArticleFetchedKey];
+    MWKArticle* article = note.userInfo[MWKArticleKey];
     if ([self.article.title isEqualToTitle:article.title]) {
         dispatchOnMainQueue(^{
             self.article = article;

-- 
To view, visit https://gerrit.wikimedia.org/r/225481
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0042dbf2bc7097cd3b4698216178cbe59ac4e30e
Gerrit-PatchSet: 1
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: 5.0
Gerrit-Owner: Fjalapeno <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to