Skip to content

Commit

Permalink
add subscript
Browse files Browse the repository at this point in the history
  • Loading branch information
maquannene committed May 19, 2016
1 parent ee59d3d commit defcd8e
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 14 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
# Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build
Binary file not shown.
2 changes: 1 addition & 1 deletion Demo/Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
printTime {
for i in 0 ... 2000 {
// print(" p = \(i)")
Tr.set(object: "213", forKey: "\(i)")
Tr!.set(object: "213", forKey: "\(i)")
}
}

Expand Down
32 changes: 26 additions & 6 deletions Track/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Foundation

public typealias CacheAsyncCompletion = (cache: Cache?, key: String?, object: AnyObject?) -> Void

let TrackCachePrefix: String = "com.trackcache."
public let TrackCachePrefix: String = "com.trackcache."

let TrackCacheDefauleName: String = "defauleTrackCache"
public let TrackCacheDefauleName: String = "defauleTrackCache"

public class Cache {

Expand All @@ -28,13 +28,16 @@ public class Cache {
// MARK: Public
public static let shareInstance = Cache(name: TrackCacheDefauleName)

public init(name: String!, path: String) {
public init?(name: String!, path: String) {
if name.characters.count == 0 || path.characters.count == 0 {
return nil
}
self.diskCache = DiskCache(name: name, path: path)!
self.name = name
self.memoryCache = MemoryCache.shareInstance
self.diskCache = DiskCache(name: name, path: path)
}

public convenience init(name: String){
public convenience init?(name: String){
self.init(name: name, path: NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0])
}

Expand Down Expand Up @@ -120,7 +123,24 @@ public class Cache {
diskCache.removeAllObject()
}

typealias OperationCompeltion = () -> Void
public subscript(key: String) -> NSCoding? {
get {
if let returnValue = object(forKey: key) as? NSCoding {
return returnValue
}
return nil
}
set {
if let newValue = newValue {
set(object: newValue, forKey: key)
}
else {
removeObject(forKey: key)
}
}
}

private typealias OperationCompeltion = () -> Void

// MARK:
// MARK: Pirvate
Expand Down
32 changes: 26 additions & 6 deletions Track/DiskCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import Foundation

public typealias DiskCacheAsyncCompletion = (cache: DiskCache?, key: String?, object: AnyObject?) -> Void

private func _generateFileURL(key: String, path: NSURL) -> NSURL {
return path.URLByAppendingPathComponent(key)
}

public class DiskCache {

public let name: String
Expand All @@ -38,7 +42,10 @@ public class DiskCache {
// MARK: Public
public static let shareInstance = DiskCache(name: TrackCacheDefauleName)

public init(name: String!, path: String) {
public init?(name: String!, path: String) {
if name.characters.count == 0 || path.characters.count == 0 {
return nil
}
self.name = name
self.cacheURL = NSURL(string: path)!.URLByAppendingPathComponent(TrackCachePrefix + name, isDirectory: false)

Expand All @@ -49,7 +56,7 @@ public class DiskCache {
}
}

public convenience init(name: String) {
public convenience init?(name: String) {
self.init(name: name, path: NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0])
}

Expand Down Expand Up @@ -126,6 +133,23 @@ public class DiskCache {
}
}

public subscript(key: String) -> NSCoding? {
get {
if let returnValue = object(forKey: key) as? NSCoding {
return returnValue
}
return nil
}
set {
if let newValue = newValue {
set(object: newValue, forKey: key)
}
else {
removeObject(forKey: key)
}
}
}

// MARK:
// MARK: Private
private func _createCacheDir() -> Bool {
Expand All @@ -139,10 +163,6 @@ public class DiskCache {
}
return true
}

private func _generateFileURL(key: String, path: NSURL) -> NSURL {
return path.URLByAppendingPathComponent(key)
}
}

// MARK: ThreadSafeProtocol
Expand Down
15 changes: 14 additions & 1 deletion Track/MemoryCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MemoryCache {
// MARK: Public
public static let shareInstance = MemoryCache()

init () {
public init () {

}

Expand Down Expand Up @@ -100,6 +100,19 @@ public class MemoryCache {
self.cache.removeAllObjects()
}
}

public subscript(key: String) -> AnyObject? {
get {
return object(forKey: key)
}
set {
if let newValue = newValue {
set(object: newValue, forKey: key)
} else {
removeObject(forKey: key)
}
}
}
}

// MARK: ThreadSafeProtocol
Expand Down

0 comments on commit defcd8e

Please sign in to comment.