Mknetworkkit is a very useful, lightweight network request framework. can support automatic caching.
However, in some cases, we require data to be updated in real time, such as after placing orders, order status Refresh, product status refresh, and so on.
The Mknetworkkit has a variable that controls the cache's effective time, called mknetworkkitdefaultcacheduration. This variable is pre-defined in the MKNetworkKit.h
// MKNetworkKit.h#define kMKNetworkKitDefaultCacheDuration 60 // 1 minute
We can find this variable to be used in mknetworkoperation.m after the global search.
expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultCacheDuration];
This expiresondate is the variable used to control the cache lifecycle.
If you need to customize the cache time, you can customize a mknetworkoperation subclass. Here for a simple demonstration, it is directly in the Mknetworkoperation class to modify.
First, write in the location where the MKNetworkOperation.h file defines the property.
@propertystrong) NSString *customExpireTime;//ifisnotdefaulttimewith this
and find out where this line of code is.
expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultCacheDuration];
and change it to
if (self.customExpireTime) { int expireSeconds = [self.customExpireTime intValue]; expiresOnDate = [[NSDate date] dateByAddingTimeInterval:expireSeconds];}else{ expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultCacheDuration];}
When using, if you need to customize the cache validity time, you just need to add such a line of code
netOp = [appDelegate.netEngine operationWithPath:path];//加上下面这行代码,自定义缓存有效时间.netOp.customExpireTime = [NSString stringWithFormat:@"%d",2];
Of course, in order to ensure the integrity of the source code, and to facilitate future updates of mknetworkkit, it is recommended to customize a mknetworkoperation subclass to accomplish the same function.
Mknetworkkit Custom Cache validity time