@@ -18,6 +18,9 @@ extern NSErrorUserInfoKey const PINDiskCacheErrorReadFailureCodeKey;
18
18
extern NSErrorUserInfoKey const PINDiskCacheErrorWriteFailureCodeKey;
19
19
extern NSString * const PINDiskCachePrefix;
20
20
21
+ extern NSUInteger PINDiskCacheDefaultByteLimit;
22
+ extern NSTimeInterval PINDiskCacheDefaultAgeLimit;
23
+
21
24
typedef NS_ENUM (NSInteger , PINDiskCacheError) {
22
25
PINDiskCacheErrorReadFailure = -1000 ,
23
26
PINDiskCacheErrorWriteFailure = -1001 ,
@@ -168,6 +171,11 @@ PIN_SUBCLASSING_RESTRICTED
168
171
*/
169
172
@property (assign ) NSTimeInterval ageLimit;
170
173
174
+ /* *
175
+ The eviction strategy when trimming the cache.
176
+ */
177
+ @property (atomic , assign ) PINCacheEvictionStrategy evictionStrategy;
178
+
171
179
/* *
172
180
The writing protection option used when writing a file on disk. This value is used every time an object is set.
173
181
NSDataWritingAtomic and NSDataWritingWithoutOverwriting are ignored if set
@@ -336,6 +344,33 @@ PIN_SUBCLASSING_RESTRICTED
336
344
operationQueue : (nonnull PINOperationQueue *)operationQueue
337
345
ttlCache : (BOOL )ttlCache ;
338
346
347
+ /* *
348
+ @see name
349
+ @param name The name of the cache.
350
+ @param prefix The prefix for the cache name. Defaults to com.pinterest.PINDiskCache
351
+ @param rootPath The path of the cache.
352
+ @param serializer A block used to serialize object. If nil provided, default NSKeyedArchiver serialized will be used.
353
+ @param deserializer A block used to deserialize object. If nil provided, default NSKeyedUnarchiver serialized will be used.
354
+ @param keyEncoder A block used to encode key(filename). If nil provided, default url encoder will be used
355
+ @param keyDecoder A block used to decode key(filename). If nil provided, default url decoder will be used
356
+ @param operationQueue A PINOperationQueue to run asynchronous operations
357
+ @param ttlCache Whether or not the cache should behave as a TTL cache.
358
+ @param byteLimit The maximum number of bytes allowed on disk. Defaults to 50MB.
359
+ @param ageLimit The maximum number of seconds an object is allowed to exist in the cache. Defaults to 30 days.
360
+ @result A new cache with the specified name.
361
+ */
362
+ - (instancetype )initWithName : (nonnull NSString *)name
363
+ prefix : (nonnull NSString *)prefix
364
+ rootPath : (nonnull NSString *)rootPath
365
+ serializer : (nullable PINDiskCacheSerializerBlock)serializer
366
+ deserializer : (nullable PINDiskCacheDeserializerBlock)deserializer
367
+ keyEncoder : (nullable PINDiskCacheKeyEncoderBlock)keyEncoder
368
+ keyDecoder : (nullable PINDiskCacheKeyDecoderBlock)keyDecoder
369
+ operationQueue : (nonnull PINOperationQueue *)operationQueue
370
+ ttlCache : (BOOL )ttlCache
371
+ byteLimit : (NSUInteger )byteLimit
372
+ ageLimit : (NSTimeInterval )ageLimit ;
373
+
339
374
/* *
340
375
The designated initializer allowing you to override default NSKeyedArchiver/NSKeyedUnarchiver serialization.
341
376
@@ -351,6 +386,7 @@ PIN_SUBCLASSING_RESTRICTED
351
386
@param ttlCache Whether or not the cache should behave as a TTL cache.
352
387
@param byteLimit The maximum number of bytes allowed on disk. Defaults to 50MB.
353
388
@param ageLimit The maximum number of seconds an object is allowed to exist in the cache. Defaults to 30 days.
389
+ @param evictionStrategy How the cache decides to evict objects
354
390
@result A new cache with the specified name.
355
391
*/
356
392
- (instancetype )initWithName : (nonnull NSString *)name
@@ -363,7 +399,8 @@ PIN_SUBCLASSING_RESTRICTED
363
399
operationQueue : (nonnull PINOperationQueue *)operationQueue
364
400
ttlCache : (BOOL )ttlCache
365
401
byteLimit : (NSUInteger )byteLimit
366
- ageLimit : (NSTimeInterval )ageLimit NS_DESIGNATED_INITIALIZER;
402
+ ageLimit : (NSTimeInterval )ageLimit
403
+ evictionStrategy : (PINCacheEvictionStrategy)evictionStrategy NS_DESIGNATED_INITIALIZER;
367
404
368
405
#pragma mark - Asynchronous Methods
369
406
// / @name Asynchronous Methods
@@ -471,7 +508,7 @@ PIN_SUBCLASSING_RESTRICTED
471
508
- (void )trimToSizeAsync : (NSUInteger )byteCount completion : (nullable PINCacheBlock)block ;
472
509
473
510
/* *
474
- Removes objects from the cache, ordered by date (least recently used first) , until the cache is equal to or smaller
511
+ Removes objects from the cache, using the eviction strategy , until the cache is equal to or smaller
475
512
than the specified byteCount. This method returns immediately and executes the passed block as soon as the cache has
476
513
been trimmed.
477
514
@@ -480,7 +517,7 @@ PIN_SUBCLASSING_RESTRICTED
480
517
481
518
@note This will not remove objects that have been added via one of the @c -setObject:forKey:withAgeLimit methods.
482
519
*/
483
- - (void )trimToSizeByDateAsync : (NSUInteger )byteCount completion : (nullable PINCacheBlock)block ;
520
+ - (void )trimToSizeByEvictionStrategyAsync : (NSUInteger )byteCount completion : (nullable PINCacheBlock)block ;
484
521
485
522
/* *
486
523
Loops through all objects in the cache (reads and writes are suspended during the enumeration). Data is not actually
@@ -564,15 +601,15 @@ PIN_SUBCLASSING_RESTRICTED
564
601
- (void )trimToSize : (NSUInteger )byteCount ;
565
602
566
603
/* *
567
- Removes objects from the cache, ordered by date (least recently used first) , until the cache is equal to or
604
+ Removes objects from the cache, using the defined evictionStrategy , until the cache is equal to or
568
605
smaller than the specified byteCount. This method blocks the calling thread until the cache has been trimmed.
569
606
570
- @see trimToSizeByDateAsync :
607
+ @see trimToSizeByEvictionStrategyAsync :
571
608
@param byteCount The cache will be trimmed equal to or smaller than this size.
572
609
573
610
@note This will not remove objects that have been added via one of the @c -setObject:forKey:withAgeLimit methods.
574
611
*/
575
- - (void )trimToSizeByDate : (NSUInteger )byteCount ;
612
+ - (void )trimToSizeByEvictionStrategy : (NSUInteger )byteCount ;
576
613
577
614
/* *
578
615
Loops through all objects in the cache (reads and writes are suspended during the enumeration). Data is not actually
@@ -616,6 +653,8 @@ typedef void (^PINDiskCacheBlock)(PINDiskCache *cache);
616
653
- (void )removeAllObjects : (nullable PINDiskCacheBlock)block __attribute__((deprecated));
617
654
- (void )enumerateObjectsWithBlock : (PINDiskCacheFileURLBlock)block completionBlock : (nullable PINDiskCacheBlock)completionBlock __attribute__((deprecated));
618
655
- (void )setTtlCache : (BOOL )ttlCache DEPRECATED_MSG_ATTRIBUTE(" ttlCache is no longer a settable property and must now be set via initializer." );
656
+ - (void )trimToSizeByDate : (NSUInteger )byteCount DEPRECATED_MSG_ATTRIBUTE(" Use trimToSizeByEvictionStrategy: instead" );
657
+ - (void )trimToSizeByDateAsync : (NSUInteger )byteCount completion : (nullable PINCacheBlock)block DEPRECATED_MSG_ATTRIBUTE(" Use trimToSizeByEvictionStrategyAsync:completion: instead" );
619
658
@end
620
659
621
660
NS_ASSUME_NONNULL_END
0 commit comments