Product Promotion
0x5a.live
for different kinds of informations and explorations.
GitHub - yysskk/MemoryCache: LRU, type-safe, thread-safe memory cache class in Swift
LRU, type-safe, thread-safe memory cache class in Swift - yysskk/MemoryCache
Visit SiteGitHub - yysskk/MemoryCache: LRU, type-safe, thread-safe memory cache class in Swift
LRU, type-safe, thread-safe memory cache class in Swift - yysskk/MemoryCache
Powered by 0x5a.live ๐
MemoryCache
Overview
MemoryCache is a memory cache class in swift.
- The MemoryCache class incorporates LRU policies, which ensure that a cache doesnโt use too much of the systemโs memory. If memory is needed by other applications, it removes some items from the cache, minimizing its memory footprint.
- You can add, remove, and query items with expiration in the cache from different threads without having to lock the cache yourself. ( thread-safe )
- Unlike the NSCache class, the cache guarantees a type by its key. ( type-safe )
let memoryCache = MemoryCache.default // or initialize
// Defining a string (or hash) key for a dog value.
let dogKey = StringKey<Dog>("dog")
// Setting a dog value in memoryCache.
memoryCache.set(dog, for: dogKey)
// Getting a cached dog value in memoryCache.
let cachedDog = try? memoryCache.value(for: dogKey)
// Removing a cached dog value in memoryCache.
memoryCache.remove(for: dogKey)
Usage
Basic
Defining keys
let dogKey = StringKey<Dog>("dog")
Adding a Cached Value
memoryCache.set(dog, for: dogKey)
Getting a Cached Value
let dog = try? memoryCache.value(for: dogKey)
Removing Cached Values
- Removes the cache of the specified key.
memoryCache.remove(for: dogKey)
- Removes the cache of the specified key if it expired.
memoryCache.removeIfExpired(for: dogKey)
- Removes All.
memoryCache.removeAll()
Others
Properties
/// The maximum total cost that the memoryCache can hold before it starts evicting caches.
var totalCostLimit: Int
/// The maximum number of caches the memoryCache should hold.
var countLimit: Int
/// The total cost of values in the memoryCache.
var totalCost: Int
/// The number of values in the memoryCache.
var count: Int
/// A Boolean value indicating whether the memoryCache has no values.
var isEmpty: Bool
Implement delegate
import MemoryCache
class SomeClass: NSObject, MemoryCacheDelegate {
let memoryCache: MemoryCache
init() {
memoryCache = MemoryCache.default
...
super.init()
memoryCache.delegate = self
}
func memoryCache(_ memoryCache: MemoryCache, willEvict cache: Any) {
// Called when an cache is about to be evicted or removed from the memoryCache.
}
}
Expiration date
You can specify expiration date for cache. The default expiration is .never
.
/// The expiration date is `.never`.
memoryCache.set(dog, for: dogKey, expiration: .never)
/// The expiration date is `.seconds("""10s""")`.
memoryCache.set(dog, for: dogKey, expiration: .seconds(10))
/// The expiration date is `.date("""TOMORROW""")`.
memoryCache.set(dog, for: dogKey, expiration: .date(Date().addingTimeInterval(60 * 60 * 24)))
/// Remove the cache of the specified key if it expired.
memoryCache.removeIfExpired(for: dogKey)
Requirements
- Swift 4.2 ~
- Xcode 10.1 ~
Installation
CocoaPods
MemoryCache is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'MemoryCache'
Carthage
You can integrate via Carthage, too.
Add the following line to your Cartfile
:
github "yysskk/MemoryCache"
and run carthage update
Author
Yusuke Morishita
License
MemoryCache
is available under the MIT license. See the LICENSE file for more info.
Swift Resources
are all listed below.
GitHub - davdroman/TextBuilder: Like a SwiftUI ViewBuilder, but for Text
resource
~/github.com
resource
GitHub - tadejr/ResizingTokenField: A token field implementation for iOS
resource
~/github.com
resource
GitHub - canalesb93/MantleModal: A draggable modal for iOS Applications.
resource
~/github.com
resource
GitHub - okmr-d/DOFavoriteButton: Cute Animated Button written in Swift.
resource
~/github.com
resource
GitHub - gmertk/GMStepper: A stepper with a sliding label in the middle.
resource
~/github.com
resource
GitHub - realm/SwiftLint: A tool to enforce Swift style and conventions.
resource
~/github.com
resource
GitHub - kciter/HorizontalDial: A horizontal scroll dial like Instagram.
resource
~/github.com
resource
GitHub - HeroTransitions/Hero: Elegant transition library for iOS & tvOS
resource
~/github.com
resource
GitHub - Brightify/Cuckoo: Boilerplate-free mocking framework for Swift!
resource
~/github.com
resource
GitHub - maxep/MXParallaxHeader: Simple parallax header for UIScrollView
resource
~/github.com
resource
GitHub - exyte/PopupView: Toasts and popups library written with SwiftUI
resource
~/github.com
resource
GitHub - gkaimakas/SwiftValidators: String (and more) validation for iOS
resource
~/github.com
resource
GitHub - alexliubj/EZAnchor: An easier and faster way to code Autolayout
resource
~/github.com
resource
GitHub - Brightify/Reactant: Reactant is a reactive architecture for iOS
resource
~/github.com
resource
GitHub - Nero5023/CSVParser: A swift package for read and write CSV file
resource
~/github.com
resource
GitHub - andybest/linenoise-swift: A pure Swift replacement for readline
resource
~/github.com
resource
GitHub - s2mr/xc: Open your xcode project with Xcode of specific version
resource
~/github.com
resource
GitHub - marmelroy/Zip: Swift framework for zipping and unzipping files.
resource
~/github.com
resource
GitHub - yonaskolb/Beak: A command line interface for your Swift scripts
resource
~/github.com
resource
GitHub - Polidea/RxBluetoothKit: iOS & OSX Bluetooth library for RxSwift
resource
~/github.com
resource
Made with โค๏ธ
to provide different kinds of informations and resources.