From 36a2199398a1a80d8a47d1a01476c0d3bf453a11 Mon Sep 17 00:00:00 2001 From: BM Date: Tue, 23 Jul 2019 17:58:31 +0800 Subject: [PATCH 1/2] [add][MemoryCache] add `LinkedMap` , `LinkedMapNode` Class and LinkedMap method --- Sources/ClaretCache/MemoryCache.swift | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Sources/ClaretCache/MemoryCache.swift diff --git a/Sources/ClaretCache/MemoryCache.swift b/Sources/ClaretCache/MemoryCache.swift new file mode 100644 index 0000000..5799663 --- /dev/null +++ b/Sources/ClaretCache/MemoryCache.swift @@ -0,0 +1,77 @@ +// +// 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() { + + } +} From 6ba37bf557817b17d540b711024a57b94d1a979a Mon Sep 17 00:00:00 2001 From: BM Date: Tue, 23 Jul 2019 18:57:54 +0800 Subject: [PATCH 2/2] [update][MemoryCache] swiftlint and code style --- .swiftlint.yml | 2 +- ClaretCacheDemo.xcodeproj/project.pbxproj | 47 ++++++++++++++++++- .../contents.xcworkspacedata | 3 ++ Sources/ClaretCache/MemoryCache.swift | 13 +---- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index d28769f..c96aad4 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -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 diff --git a/ClaretCacheDemo.xcodeproj/project.pbxproj b/ClaretCacheDemo.xcodeproj/project.pbxproj index 22464ce..f01a0c2 100644 --- a/ClaretCacheDemo.xcodeproj/project.pbxproj +++ b/ClaretCacheDemo.xcodeproj/project.pbxproj @@ -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 */ @@ -23,6 +25,8 @@ 01C75DF222E0724000C7D03F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 01C75DF522E0724000C7D03F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 01C75DF722E0724000C7D03F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 01FA686B22E719DB008E24FC /* MemoryCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryCache.swift; sourceTree = ""; }; + 01FA686C22E719DB008E24FC /* ClaretCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClaretCache.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -59,6 +63,7 @@ 01C75DEB22E0723E00C7D03F /* AppDelegate.swift */, 01C75DED22E0723E00C7D03F /* ViewController.swift */, 01C75DEF22E0723E00C7D03F /* Main.storyboard */, + 01FA686922E719DB008E24FC /* Sources */, 01C75DF222E0724000C7D03F /* Assets.xcassets */, 01C75DF422E0724000C7D03F /* LaunchScreen.storyboard */, 01C75DF722E0724000C7D03F /* Info.plist */, @@ -66,6 +71,23 @@ path = ClaretCacheDemo; sourceTree = ""; }; + 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 = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -76,6 +98,7 @@ 01C75DE422E0723E00C7D03F /* Sources */, 01C75DE522E0723E00C7D03F /* Frameworks */, 01C75DE622E0723E00C7D03F /* Resources */, + 01FA686F22E719F3008E24FC /* ShellScript */, ); buildRules = ( ); @@ -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; }; @@ -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; diff --git a/ClaretCacheDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ClaretCacheDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 94b2795..919434a 100644 --- a/ClaretCacheDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/ClaretCacheDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -1,4 +1,7 @@ + + diff --git a/Sources/ClaretCache/MemoryCache.swift b/Sources/ClaretCache/MemoryCache.swift index 5799663..d398efc 100644 --- a/Sources/ClaretCache/MemoryCache.swift +++ b/Sources/ClaretCache/MemoryCache.swift @@ -35,43 +35,32 @@ class LinkedMap { 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); + 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() { - } }