Skip to content

Commit

Permalink
Merge pull request #16 from birdmichael/bm_dev
Browse files Browse the repository at this point in the history
[add][MemoryCache] add `LinkedMap` , `LinkedMapNode` Class and Linked…
  • Loading branch information
0x1306a94 authored Jul 24, 2019
2 parents 996b7fc + 6ba37bf commit 6015640
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type_name:
excluded: iPhone # excluded via string
identifier_name:
min_length: # only min_length
error: 4 # only error
error: 2 # only error
excluded: # excluded via string array
- id
- URL
Expand Down
47 changes: 46 additions & 1 deletion ClaretCacheDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
01C75DF122E0723E00C7D03F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 01C75DEF22E0723E00C7D03F /* Main.storyboard */; };
01C75DF322E0724000C7D03F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 01C75DF222E0724000C7D03F /* Assets.xcassets */; };
01C75DF622E0724000C7D03F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 01C75DF422E0724000C7D03F /* LaunchScreen.storyboard */; };
01FA686D22E719DB008E24FC /* MemoryCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01FA686B22E719DB008E24FC /* MemoryCache.swift */; };
01FA686E22E719DB008E24FC /* ClaretCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01FA686C22E719DB008E24FC /* ClaretCache.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -23,6 +25,8 @@
01C75DF222E0724000C7D03F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
01C75DF522E0724000C7D03F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
01C75DF722E0724000C7D03F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
01FA686B22E719DB008E24FC /* MemoryCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryCache.swift; sourceTree = "<group>"; };
01FA686C22E719DB008E24FC /* ClaretCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClaretCache.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -59,13 +63,31 @@
01C75DEB22E0723E00C7D03F /* AppDelegate.swift */,
01C75DED22E0723E00C7D03F /* ViewController.swift */,
01C75DEF22E0723E00C7D03F /* Main.storyboard */,
01FA686922E719DB008E24FC /* Sources */,
01C75DF222E0724000C7D03F /* Assets.xcassets */,
01C75DF422E0724000C7D03F /* LaunchScreen.storyboard */,
01C75DF722E0724000C7D03F /* Info.plist */,
);
path = ClaretCacheDemo;
sourceTree = "<group>";
};
01FA686922E719DB008E24FC /* Sources */ = {
isa = PBXGroup;
children = (
01FA686A22E719DB008E24FC /* ClaretCache */,
);
path = Sources;
sourceTree = SOURCE_ROOT;
};
01FA686A22E719DB008E24FC /* ClaretCache */ = {
isa = PBXGroup;
children = (
01FA686B22E719DB008E24FC /* MemoryCache.swift */,
01FA686C22E719DB008E24FC /* ClaretCache.swift */,
);
path = ClaretCache;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -76,6 +98,7 @@
01C75DE422E0723E00C7D03F /* Sources */,
01C75DE522E0723E00C7D03F /* Frameworks */,
01C75DE622E0723E00C7D03F /* Resources */,
01FA686F22E719F3008E24FC /* ShellScript */,
);
buildRules = (
);
Expand Down Expand Up @@ -138,13 +161,35 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
01FA686F22E719F3008E24FC /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 12;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "# Type a script or drag a script file from your workspace to insert its path.\nif which swiftlint >/dev/null; then\nswiftlint\nelse\necho \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
01C75DE422E0723E00C7D03F /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
01FA686D22E719DB008E24FC /* MemoryCache.swift in Sources */,
01C75DEE22E0723E00C7D03F /* ViewController.swift in Sources */,
01C75DEC22E0723E00C7D03F /* AppDelegate.swift in Sources */,
01FA686E22E719DB008E24FC /* ClaretCache.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -350,7 +395,7 @@
/* Begin XCRemoteSwiftPackageReference section */
01ADE0E422E08661005DC3D4 /* XCRemoteSwiftPackageReference "ClaretCache" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/iteatimeteam/ClaretCache.git";
repositoryURL = "https://github.com/iteatimeteam/ClaretCache.git";
requirement = {
branch = master;
kind = branch;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions Sources/ClaretCache/MemoryCache.swift
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() {
}
}

0 comments on commit 6015640

Please sign in to comment.