<commit_before>//
//  CacheAdapter.swift
//  iAsync_cache
//
//  Created by <NAME> on 05.08.14.
//  Copyright (c) 2014 EmbeddedSources. All rights reserved.
//

import Foundation

import iAsync_utils
import iAsync_restkit
import iAsync_reactiveKit

import ReactiveKit

public typealias CacheFactory = () -> CacheDB

public class CacheAdapter : AsyncRestKitCache {

    private let cacheFactory  : CacheFactory
    private let cacheQueueName: String

    public init(cacheFactory: CacheFactory, cacheQueueName: String) {

        self.cacheQueueName = cacheQueueName
        self.cacheFactory   = cacheFactory
    }

    public func loaderToSetData(data: NSData, forKey key: String) -> AsyncStream<Void, AnyObject, NSError> {

        return asyncStreamWithJob(cacheQueueName, job: { (progress: AnyObject -> Void) -> Result<Void, NSError> in

            self.cacheFactory().setData(data, forKey:key)
            return .Success(())
        })
    }

    public func cachedDataLoaderForKey(key: String) -> AsyncStream<(date: NSDate, data: NSData), AnyObject, NSError> {

        return asyncStreamWithJob(cacheQueueName, job: { (progress: AnyObject -> Void) -> Result<(date: NSDate, data: NSData), NSError> in

            let result = self.cacheFactory().dataAndLastUpdateDateForKey(key)

            if let result = result {
                return .Success((date: result.1, data: result.0))
            }

            let description = "no cached data for key: \(key)"
            return .Failure(SilentError(description:description))
        })
    }
}<commit_msg>refactor with new cache logic<commit_after>//
//  CacheAdapter.swift
//  iAsync_cache
//
//  Created by <NAME> on 05.08.14.
//  Copyright (c) 2014 EmbeddedSources. All rights reserved.
//

import Foundation

import iAsync_utils
import iAsync_restkit
import iAsync_reactiveKit

import ReactiveKit

public typealias CacheFactory = () -> CacheDB

public class CacheAdapter : AsyncRestKitCache {

    private let cacheFactory  : CacheFactory
    private let cacheQueueName: String

    public init(cacheFactory: CacheFactory, cacheQueueName: String) {

        self.cacheQueueName = cacheQueueName
        self.cacheFactory   = cacheFactory
    }

    public func loaderToSetData(data: NSData, forKey key: String) -> AsyncStream<Void, AnyObject, NSError> {

        return asyncStreamWithJob(cacheQueueName, job: { _ -> Result<Void, NSError> in

            self.cacheFactory().setData(data, forKey:key)
            return .Success(())
        })
    }

    public func cachedDataLoaderForKey(key: String) -> AsyncStream<(date: NSDate, data: NSData), AnyObject, NSError> {

        return asyncStreamWithJob(cacheQueueName, job: { _ -> Result<(date: NSDate, data: NSData), NSError> in

            let result = self.cacheFactory().dataAndLastUpdateDateForKey(key)

            if let result = result {
                return .Success((date: result.1, data: result.0))
            }

            let description = "no cached data for key: \(key)"
            return .Failure(SilentError(description:description))
        })
    }
}
{'fraction_non_alphanumeric': 0.10025873221216042, 'fraction_numerical': 0.007761966364812419, 'mean_word_length': 2.9401273885350316, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7140382', 'n_tokens_mistral': 920, 'n_tokens_neox': 872, 'n_words': 232}