LRU Cache的标准实现

时间:2023-01-26 04:51:49

I'm building an application with Swift and I'd like to use an LRU Cache in my application. I've implemented a simple LRUCache<K: Hashable, V> in Swift but then I figured that since it already ships with Dictionary and Array collections I might be missing a better native option.

我正在用Swift构建一个应用程序,我想在我的应用程序中使用LRU Cache。我已经在Swift中实现了一个简单的LRUCache ,但后来我认为因为它已经附带了Dictionary和Array集合,所以我可能会错过一个更好的原生选项。 :hashable,v>

I've checked the docs and other questions and couldn't find anything relevant.

我检查了文档和其他问题,找不到任何相关的内容。

So my quesiton is: Does Swift ship with an LRUCache? If it does, how do I use it, if it doesn't: Can I utilize an ObjectiveC version and still maintain my Swift type safety?

所以我的问题是:Swift是否带有LRUCache?如果是,如何使用它,如果没有:我可以使用ObjectiveC版本并仍然保持我的Swift类型安全吗?

5 个解决方案

#1


7  

Wrapping NSCache(for type constraint) is not so hard work.

包装NSCache(用于类型约束)并不是那么辛苦。

struct LRUCache<K:AnyObject, V:AnyObject> {

    private let _cache = NSCache()

    var countLimit:Int {
        get {
            return _cache.countLimit
        }
        nonmutating set(countLimit) {
            _cache.countLimit = countLimit
        }
    }
    subscript(key:K!) -> V? {
        get {
            let obj:AnyObject? = _cache.objectForKey(key)
            return obj as V?
        }
        nonmutating set(obj) {
            if(obj == nil) {
                _cache.removeObjectForKey(key)
            }
            else {
                _cache.setObject(obj!, forKey: key)
            }
        }
    }
}

let cache = LRUCache<NSString, NSString>()
cache.countLimit = 3
cache["key1"] = "val1"
cache["key2"] = "val2"
cache["key3"] = "val3"
cache["key4"] = "val4"
cache["key5"] = "val5"
let val3 = cache["key3"]
cache["key6"] = "val6"

println((
    cache["key1"],
    cache["key2"],
    cache["key3"],
    cache["key4"],
    cache["key5"],
    cache["key6"]
))

result:

结果:

(nil, nil, Optional(val3), nil, Optional(val5), Optional(val6))

#2


2  

You could use HanekeSwift, it's a nice generic cache library written in Swift.

你可以使用HanekeSwift,它是一个用Swift编写的漂亮的通用缓存库。

#3


1  

There isn't a standard implementation of LRUCache in the Swift main libraries, nor is there one in frameworks like (Core)Foundation.

Swift主库中没有LRUCache的标准实现,在(Core)Foundation这样的框架中也没有。

#4


0  

You can use NSOrderedSet which combines NSArray and NSSet for a start; creating a LRU cache from that is quite trivial. Or for many uses, you can use NSCache.

你可以使用结合了NSArray和NSSet的NSOrderedSet作为开始;从中创建LRU缓存非常简单。或者对于许多用途,您可以使用NSCache。

#5


0  

Take a look at SwiftlyLRU on GitHub, it's a pure Swift implementation. Time: O(1), Space: O(1) which assumes no collisions into the internal hashtable and is a single file to drag into target project.

看看GitHub上的SwiftlyLRU,它是一个纯粹的Swift实现。时间:O(1),空格:O(1),它假定没有冲突到内部哈希表中,并且是一个拖入目标项目的文件。

https://github.com/justinmfischer/SwiftlyLRU

https://github.com/justinmfischer/SwiftlyLRU

#1


7  

Wrapping NSCache(for type constraint) is not so hard work.

包装NSCache(用于类型约束)并不是那么辛苦。

struct LRUCache<K:AnyObject, V:AnyObject> {

    private let _cache = NSCache()

    var countLimit:Int {
        get {
            return _cache.countLimit
        }
        nonmutating set(countLimit) {
            _cache.countLimit = countLimit
        }
    }
    subscript(key:K!) -> V? {
        get {
            let obj:AnyObject? = _cache.objectForKey(key)
            return obj as V?
        }
        nonmutating set(obj) {
            if(obj == nil) {
                _cache.removeObjectForKey(key)
            }
            else {
                _cache.setObject(obj!, forKey: key)
            }
        }
    }
}

let cache = LRUCache<NSString, NSString>()
cache.countLimit = 3
cache["key1"] = "val1"
cache["key2"] = "val2"
cache["key3"] = "val3"
cache["key4"] = "val4"
cache["key5"] = "val5"
let val3 = cache["key3"]
cache["key6"] = "val6"

println((
    cache["key1"],
    cache["key2"],
    cache["key3"],
    cache["key4"],
    cache["key5"],
    cache["key6"]
))

result:

结果:

(nil, nil, Optional(val3), nil, Optional(val5), Optional(val6))

#2


2  

You could use HanekeSwift, it's a nice generic cache library written in Swift.

你可以使用HanekeSwift,它是一个用Swift编写的漂亮的通用缓存库。

#3


1  

There isn't a standard implementation of LRUCache in the Swift main libraries, nor is there one in frameworks like (Core)Foundation.

Swift主库中没有LRUCache的标准实现,在(Core)Foundation这样的框架中也没有。

#4


0  

You can use NSOrderedSet which combines NSArray and NSSet for a start; creating a LRU cache from that is quite trivial. Or for many uses, you can use NSCache.

你可以使用结合了NSArray和NSSet的NSOrderedSet作为开始;从中创建LRU缓存非常简单。或者对于许多用途,您可以使用NSCache。

#5


0  

Take a look at SwiftlyLRU on GitHub, it's a pure Swift implementation. Time: O(1), Space: O(1) which assumes no collisions into the internal hashtable and is a single file to drag into target project.

看看GitHub上的SwiftlyLRU,它是一个纯粹的Swift实现。时间:O(1),空格:O(1),它假定没有冲突到内部哈希表中,并且是一个拖入目标项目的文件。

https://github.com/justinmfischer/SwiftlyLRU

https://github.com/justinmfischer/SwiftlyLRU