-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from birdmichael/bm_dev
[add][MemoryCache] add `LinkedMap` , `LinkedMapNode` Class and Linked…
- Loading branch information
Showing
4 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
ClaretCacheDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// MemoryCache.swift | ||
// ClaretCache | ||
// | ||
// Created by BirdMichael on 2019/7/23. | ||
// Copyright © 2019 com.ClaretCache. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
A node in linked map. | ||
Typically, you should not use this class directly. | ||
*/ | ||
class LinkedMapNode { | ||
private var prev: LinkedMapNode? | ||
private var next: LinkedMapNode? | ||
private var key: Any? | ||
private var value: Any? | ||
private var cost: UInt = 0 | ||
private var time: TimeInterval = 0.0 | ||
} | ||
|
||
/** | ||
A linked map used by MemoryCache. | ||
It's not thread-safe and does not validate the parameters. | ||
|
||
Typically, you should not use this class directly. | ||
*/ | ||
class LinkedMap { | ||
private var dic: CFMutableDictionary | ||
private var totalCost: UInt = 0 | ||
private var totalCount: UInt = 0 | ||
private var head: LinkedMapNode? // MRU, do not change it directly | ||
private var tail: LinkedMapNode? // LRU, do not change it directly | ||
private var releaseOnMainThread: Bool = false | ||
private var releaseAsynchronously: Bool = true | ||
init() { | ||
var keyCallbacks = kCFTypeDictionaryKeyCallBacks | ||
var valueCallbacks = kCFTypeDictionaryValueCallBacks | ||
dic = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallbacks, &valueCallbacks) | ||
} | ||
} | ||
|
||
extension LinkedMap { | ||
/// Insert a node at head and update the total cost. | ||
/// Node and node.key should not be nil. | ||
func insert(atHead node: LinkedMapNode) { | ||
} | ||
/// Bring a inner node to header. | ||
/// Node should already inside the dic. | ||
func bring(toHead node: LinkedMapNode) { | ||
} | ||
/// Remove a inner node and update the total cost. | ||
/// Node should already inside the dic. | ||
func remove(_ node: LinkedMapNode) { | ||
} | ||
/// Remove tail node if exist. | ||
func removeTail() -> LinkedMapNode? { | ||
let tempTail = tail | ||
return tempTail | ||
} | ||
/// Remove all node in background queue. | ||
func removeAll() { | ||
} | ||
} |