Skip to content

Commit a369de2

Browse files
committed
improved compatibility: ibireme#10
1 parent 487c2f6 commit a369de2

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

YYCache/YYCache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ FOUNDATION_EXPORT const unsigned char YYCacheVersionString[];
5555
the app's caches dictionary for disk cache. Once initialized you should not
5656
read and write to this directory.
5757
@result A new cache object, or nil if an error occurs.
58-
@warning Multiple instances with the same name will make the storage unstable.
5958
*/
6059
- (instancetype)initWithName:(NSString *)name;
6160

@@ -66,7 +65,6 @@ FOUNDATION_EXPORT const unsigned char YYCacheVersionString[];
6665
@param path Full path of a directory in which the cache will write data.
6766
Once initialized you should not read and write to this directory.
6867
@result A new cache object, or nil if an error occurs.
69-
@warning Multiple instances with the same path will make the storage unstable.
7068
*/
7169
- (instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
7270

YYCache/YYDiskCache.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@
145145
146146
@return A new cache object, or nil if an error occurs.
147147
148-
@warning Multiple instances with the same path will make the storage unstable.
148+
@warning If the cache instance for the specified path already exists in memory,
149+
this method will return it directly, instead of creating a new instance.
149150
*/
150151
- (instancetype)initWithPath:(NSString *)path;
151152

@@ -164,7 +165,8 @@
164165
165166
@return A new cache object, or nil if an error occurs.
166167
167-
@warning Multiple instances with the same path will make the storage unstable.
168+
@warning If the cache instance for the specified path already exists in memory,
169+
this method will return it directly, instead of creating a new instance.
168170
*/
169171
- (instancetype)initWithPath:(NSString *)path
170172
inlineThreshold:(NSUInteger)threshold NS_DESIGNATED_INITIALIZER;

YYCache/YYDiskCache.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#import "YYDiskCache.h"
1313
#import "YYKVStorage.h"
14+
#import <libkern/OSAtomic.h>
1415
#import <CommonCrypto/CommonCrypto.h>
1516
#import <objc/runtime.h>
1617
#import <time.h>
@@ -45,6 +46,36 @@ static int64_t _YYDiskSpaceFree() {
4546
];
4647
}
4748

49+
/// weak reference for all instances
50+
static NSMapTable *_globalInstances;
51+
static OSSpinLock _globalInstancesLock;
52+
53+
static void _YYDiskCacheInitGlobal() {
54+
static dispatch_once_t onceToken;
55+
dispatch_once(&onceToken, ^{
56+
_globalInstancesLock = OS_SPINLOCK_INIT;
57+
_globalInstances = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
58+
});
59+
}
60+
61+
static YYDiskCache *_YYDiskCacheGetGlobal(NSString *path) {
62+
if (path.length == 0) return nil;
63+
_YYDiskCacheInitGlobal();
64+
OSSpinLockLock(&_globalInstancesLock);
65+
id cache = [_globalInstances objectForKey:path];
66+
OSSpinLockUnlock(&_globalInstancesLock);
67+
return cache;
68+
}
69+
70+
static void _YYDiskCacheSetGlobal(YYDiskCache *cache) {
71+
if (cache.path.length == 0) return;
72+
_YYDiskCacheInitGlobal();
73+
OSSpinLockLock(&_globalInstancesLock);
74+
[_globalInstances setObject:cache forKey:cache.path];
75+
OSSpinLockUnlock(&_globalInstancesLock);
76+
}
77+
78+
4879

4980
@implementation YYDiskCache {
5081
YYKVStorage *_kv;
@@ -135,6 +166,9 @@ - (instancetype)initWithPath:(NSString *)path
135166
self = [super init];
136167
if (!self) return nil;
137168

169+
YYDiskCache *globalCache = _YYDiskCacheGetGlobal(path);
170+
if (globalCache) return globalCache;
171+
138172
YYKVStorageType type;
139173
if (threshold == 0) {
140174
type = YYKVStorageTypeFile;
@@ -159,6 +193,7 @@ - (instancetype)initWithPath:(NSString *)path
159193
_autoTrimInterval = 60;
160194

161195
[self _trimRecursively];
196+
_YYDiskCacheSetGlobal(self);
162197
return self;
163198
}
164199

YYCache/YYKVStorage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
/**
1515
YYKVStorageItem is used by `YYKVStorage` to store key-value pair and meta data.
16+
Typically, you should not use this class directly.
1617
*/
1718
@interface YYKVStorageItem : NSObject
1819
@property (nonatomic, strong) NSString *key; ///< key
@@ -56,6 +57,7 @@ typedef NS_ENUM(NSUInteger, YYKVStorageType) {
5657

5758
/**
5859
YYKVStorage is a key-value storage based on sqlite and file system.
60+
Typically, you should not use this class directly.
5961
6062
@discussion The designated initializer for YYKVStorage is `initWithPath:type:`.
6163
After initialized, a directory is created based on the `path` to hold key-value data.

YYCache/YYMemoryCache.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
#if __has_include("YYDispatchQueuePool.h")
2020
#import "YYDispatchQueuePool.h"
21-
#else
22-
#import <libkern/OSAtomic.h>
2321
#endif
2422

2523
#ifdef YYDispatchQueuePool_h

0 commit comments

Comments
 (0)