Skip to content

Commit 745167a

Browse files
author
pixel-ink
committed
configをわかりやすく
1 parent a694dd2 commit 745167a

4 files changed

Lines changed: 29 additions & 21 deletions

File tree

PIImageCache/PIImageCache/PIImageCache.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ public class PIImageCache {
101101

102102
private var config: Config = Config()
103103
public class Config {
104-
public var maxCount : Int = 10 // 10 images
105-
public var maxByteSize : Int = 3 * 1024 * 1024 //3MB
106-
public var usingDiskCache = true
107-
public var diskCacheExpireMinutes = 24 * 60 // 1 day
108-
public var cacheRootDirectory = NSTemporaryDirectory()
109-
public var cacheFolderName = "PIImageCache"
104+
public var maxMemorySum : Int = 10 // 10 images
105+
public var limitByteSize : Int = 3 * 1024 * 1024 //3MB
106+
public var usingDiskCache : Bool = true
107+
public var diskCacheExpireMinutes : Int = 24 * 60 // 1 day
108+
public var cacheRootDirectory : String = NSTemporaryDirectory()
109+
public var cacheFolderName : String = "PIImageCache"
110110
}
111111

112112
public func setConfig(config :Config) {
@@ -132,9 +132,9 @@ public class PIImageCache {
132132

133133
private func memoryCacheWrite(url:NSURL,image:UIImage) {
134134
switch memoryCache.count {
135-
case 0 ... config.maxCount:
135+
case 0 ... config.maxMemorySum:
136136
memoryCache.append(memoryCacheImage(image: image, timeStamp: now, url: url))
137-
case config.maxCount + 1://+1 because 0 origin
137+
case config.maxMemorySum + 1://+1 because 0 origin
138138
var old = (0,now)
139139
for i in 0 ..< memoryCache.count {
140140
if old.1 < memoryCache[i].timeStamp {
@@ -269,7 +269,7 @@ public class PIImageCache {
269269
//download
270270
let maybeImage = download(url)
271271
if let (image, byteSize) = maybeImage {
272-
if byteSize < config.maxByteSize {
272+
if byteSize < config.limitByteSize {
273273
//write memory
274274
dispatch_semaphore_wait(memorySemaphore, DISPATCH_TIME_FOREVER)
275275
memoryCacheWrite(url, image: image)

PIImageCache/PIImageCacheTests/PIImageCacheBasicTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class PIImageBasicCacheTests: XCTestCase {
2525
image = cache.get(url)!
2626
XCTAssert(image!.size.width == 200 && image!.size.height == 200 , "Pass")
2727
var config = PIImageCache.Config()
28-
config.maxCount = 5
29-
config.maxByteSize = 100 * 1024 // 100kB
28+
config.maxMemorySum = 5
29+
config.limitByteSize = 100 * 1024 // 100kB
3030
cache.setConfig(config)
3131
image = cache.get(url)!
3232
XCTAssert(image!.size.width == 200 && image!.size.height == 200 , "Pass")

PIImageCache/PIImageCacheTests/PIImageMemoryCacheTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ class PIImageMemoryCacheTests: XCTestCase {
8787

8888
func testCacheMaxCount() {
8989
let config = PIImageCache.Config()
90-
config.maxCount = 5
91-
config.maxByteSize = 3 * 1024 * 1024
90+
config.maxMemorySum = 5
91+
config.limitByteSize = 3 * 1024 * 1024
9292
let cache = PIImageCache(config: config)
9393
var image: UIImage?, result: PIImageCache.Result
9494
var urls :[NSURL] = []
@@ -114,8 +114,8 @@ class PIImageMemoryCacheTests: XCTestCase {
114114

115115
func testCacheMaxSize() {
116116
let config = PIImageCache.Config()
117-
config.maxCount = 10
118-
config.maxByteSize = 100
117+
config.maxMemorySum = 10
118+
config.limitByteSize = 100
119119
let cache = PIImageCache(config: config)
120120
let url = NSURL(string: "http://place-hold.it/200x200")!
121121
var image: UIImage?, result: PIImageCache.Result
@@ -202,7 +202,7 @@ class PIImageMemoryCacheTests: XCTestCase {
202202

203203
var config = PIImageCache.Config()
204204
config.usingDiskCache = false
205-
config.maxCount = 5
205+
config.maxMemorySum = 5
206206
var cache = PIImageCache(config: config)
207207

208208
for i in 0 ..< 5 {
@@ -217,7 +217,7 @@ class PIImageMemoryCacheTests: XCTestCase {
217217
XCTAssert(image!.size.width == 200 && image!.size.height == 200 , "Pass")
218218
}
219219

220-
config.maxCount = 2
220+
config.maxMemorySum = 2
221221
cache = PIImageCache(config: config)
222222

223223
for i in 0 ..< 2 {
@@ -232,7 +232,7 @@ class PIImageMemoryCacheTests: XCTestCase {
232232
XCTAssert(image!.size.width == 200 && image!.size.height == 200 , "Pass")
233233
}
234234

235-
config.maxByteSize = 100
235+
config.limitByteSize = 100
236236
cache = PIImageCache(config: config)
237237

238238
for i in 3 ..< 5 {

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,18 @@ image = cache.get(url)!
7676
```Config.swift
7777
let cache = PIImageCache.shared
7878
var config = PIImageCache.Config()
79-
config.maxCount = 5
80-
config.maxByteSize = 100 * 1024 // 100kB
79+
config.maxMemorySum = 5
80+
config.limitByteSize = 100 * 1024 // 100kB
8181
cache.setConfig(config)
8282

8383
let url = NSURL(string: "http://place-hold.it/200x200")!
8484
let image = cache.get(url)!
85-
```
85+
```
86+
87+
- default values
88+
- maxMemorySum = 10 // 10 images
89+
- limitByteSize = 3 * 1024 * 1024 //3MB
90+
- usingDiskCache = true
91+
- diskCacheExpireMinutes = 24 * 60 // 1 day
92+
- cacheRootDirectory = NSTemporaryDirectory()
93+
- cacheFolderName = "PIImageCache"

0 commit comments

Comments
 (0)