All Projects β†’ yysskk β†’ Memorycache

yysskk / Memorycache

Licence: mit
LRU, type-safe, thread-safe memory cache class in Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Memorycache

Cluster
Easy Map Annotation Clustering πŸ“
Stars: ✭ 1,132 (+1615.15%)
Mutual labels:  cocoapods, carthage
Cameramanager
Simple Swift class to provide all the configurations you need to create custom camera view in your app
Stars: ✭ 1,130 (+1612.12%)
Mutual labels:  cocoapods, carthage
Swiftymessenger
Swift toolkit for passing messages between iOS apps and extensions.
Stars: ✭ 48 (-27.27%)
Mutual labels:  cocoapods, carthage
Pinpointkit
Send better feedback
Stars: ✭ 1,115 (+1589.39%)
Mutual labels:  cocoapods, carthage
Peep
Easy Sound Generator πŸ₯
Stars: ✭ 59 (-10.61%)
Mutual labels:  cocoapods, carthage
Lgalertview
Customizable implementation of UIAlertViewController, UIAlertView and UIActionSheet. All in one. You can customize every detail. Make AlertView of your dream! :)
Stars: ✭ 1,027 (+1456.06%)
Mutual labels:  cocoapods, carthage
Stravakit
Strava API Client built with Swift
Stars: ✭ 50 (-24.24%)
Mutual labels:  cocoapods, carthage
Sketchkit
A lightweight auto-layout DSL library for iOS & tvOS.
Stars: ✭ 40 (-39.39%)
Mutual labels:  cocoapods, carthage
Transitionbutton
UIButton sublass for loading and transition animation.
Stars: ✭ 1,124 (+1603.03%)
Mutual labels:  cocoapods, carthage
Mtbbarcodescanner
A lightweight, easy-to-use barcode scanning library for iOS 8+
Stars: ✭ 1,085 (+1543.94%)
Mutual labels:  cocoapods, carthage
Threadly
Type-safe thread-local storage in Swift
Stars: ✭ 58 (-12.12%)
Mutual labels:  cocoapods, carthage
Css3colorsswift
A UIColor extension with CSS3 Color names.
Stars: ✭ 62 (-6.06%)
Mutual labels:  cocoapods, carthage
Wstagsfield
An iOS text field that represents tags, hashtags, tokens in general.
Stars: ✭ 1,013 (+1434.85%)
Mutual labels:  cocoapods, carthage
Appz
πŸ“± Launch external apps, and deeplink, with ease using Swift!
Stars: ✭ 1,092 (+1554.55%)
Mutual labels:  cocoapods, carthage
Istimeline
Simple timeline view written in Swift 3
Stars: ✭ 1,005 (+1422.73%)
Mutual labels:  cocoapods, carthage
Cdalertview
Highly customizable alertview and alert/notification/success/error/alarm popup written in Swift
Stars: ✭ 1,056 (+1500%)
Mutual labels:  cocoapods, carthage
Gaugekit
Kit for building custom gauges + easy reproducible Apple's style ring gauges.
Stars: ✭ 997 (+1410.61%)
Mutual labels:  cocoapods, carthage
Fontblaster
Programmatically load custom fonts into your iOS and tvOS app.
Stars: ✭ 1,000 (+1415.15%)
Mutual labels:  cocoapods, carthage
Luautocompleteview
Highly configurable autocomplete view that is attachable to any UITextField
Stars: ✭ 55 (-16.67%)
Mutual labels:  cocoapods, carthage
Unboxedalamofire
[Deprecated] Alamofire + Unbox: the easiest way to download and decode JSON into swift objects.
Stars: ✭ 65 (-1.52%)
Mutual labels:  cocoapods, carthage

MemoryCache

CI Status codecov Version License Platform

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

Support via PayPal

License

MemoryCache is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].