1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-02 09:27:55 +01:00
Telegram/Share/TGMemoryCache.m
2015-10-01 19:19:52 +03:00

51 lines
813 B
Objective-C

#import "TGMemoryCache.h"
#import <libkern/OSAtomic.h>
@interface TGMemoryCache ()
{
OSSpinLock _lock;
NSMutableDictionary *_dict;
}
@end
@implementation TGMemoryCache
- (instancetype)init
{
self = [super init];
if (self != nil)
{
_dict = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)setValue:(id)value forKey:(NSString *)key
{
if (key != nil)
{
OSSpinLockLock(&_lock);
if (value == nil)
[_dict removeObjectForKey:key];
else
_dict[key] = value;
OSSpinLockUnlock(&_lock);
}
}
- (id)valueForKey:(NSString *)key
{
if (key != nil)
{
OSSpinLockLock(&_lock);
id value = _dict[key];
OSSpinLockUnlock(&_lock);
return value;
}
return nil;
}
@end