Skip to content

Commit 36ef824

Browse files
committed
Merge pull request #41 from hyperoslo/improve/memory-handling
Improve memory handling
2 parents ea1a831 + b06e3b3 commit 36ef824

File tree

14 files changed

+114
-20
lines changed

14 files changed

+114
-20
lines changed

.swiftlint.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
included: # paths to include during linting. `--path` is ignored if present.
2+
- Source
3+
excluded: # paths to ignore during linting. Takes precedence over `included`.
4+
- Carthage
5+
- Pods
6+
7+
# configurable rules can be customized from this configuration file
8+
# binary rules can set their severity level
9+
force_cast: warning # implicitly
10+
force_try:
11+
severity: warning # explicitly
12+
# rules that have both warning and error levels, can set just the warning level
13+
# implicitly
14+
line_length: 200
15+
# they can set both implicitly with an array
16+
type_body_length:
17+
- 300 # warning
18+
- 400 # error
19+
# or they can set both explicitly
20+
file_length:
21+
warning: 500
22+
error: 1200
23+
# naming rules can set warnings/errors for min_length and max_length
24+
# additionally they can set excluded names
25+
type_name:
26+
min_length: 3 # only warning
27+
max_length: # warning and error
28+
warning: 40
29+
error: 50
30+
excluded: iPhone # excluded via string
31+
variable_name:
32+
min_length: # only min_length
33+
error: 2 # only error
34+
excluded: # excluded via string array
35+
- x
36+
- y
37+
- id
38+
- URL
39+
- GlobalAPIKey
40+
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle)

Cache.xcodeproj/project.pbxproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@
546546
D5291D5D1C283B5300B702C9 /* Headers */,
547547
D5291D5E1C283B5300B702C9 /* Resources */,
548548
D5BAEBF41CD1F4D30003E865 /* ShellScript */,
549+
BD58C3AE1CF2EA04003F7141 /* ShellScript */,
549550
);
550551
buildRules = (
551552
);
@@ -584,6 +585,7 @@
584585
D5DC59DD1C20593E003BD79B /* Headers */,
585586
D5DC59DE1C20593E003BD79B /* Resources */,
586587
D5BAEBF21CD1F4490003E865 /* ShellScript */,
588+
BD58C3AD1CF2E9FD003F7141 /* ShellScript */,
587589
);
588590
buildRules = (
589591
);
@@ -670,6 +672,32 @@
670672
/* End PBXResourcesBuildPhase section */
671673

672674
/* Begin PBXShellScriptBuildPhase section */
675+
BD58C3AD1CF2E9FD003F7141 /* ShellScript */ = {
676+
isa = PBXShellScriptBuildPhase;
677+
buildActionMask = 2147483647;
678+
files = (
679+
);
680+
inputPaths = (
681+
);
682+
outputPaths = (
683+
);
684+
runOnlyForDeploymentPostprocessing = 0;
685+
shellPath = /bin/sh;
686+
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
687+
};
688+
BD58C3AE1CF2EA04003F7141 /* ShellScript */ = {
689+
isa = PBXShellScriptBuildPhase;
690+
buildActionMask = 2147483647;
691+
files = (
692+
);
693+
inputPaths = (
694+
);
695+
outputPaths = (
696+
);
697+
runOnlyForDeploymentPostprocessing = 0;
698+
shellPath = /bin/sh;
699+
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
700+
};
673701
D5291D591C283AA700B702C9 /* ShellScript */ = {
674702
isa = PBXShellScriptBuildPhase;
675703
buildActionMask = 2147483647;

Source/Mac/Extensions/NSImage+Cache.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extension NSImage: Cachable {
1414
Creates UIImage from NSData
1515

1616
- Parameter data: Data to decode from
17+
- Returns: Optional CacheType
1718
*/
1819
public static func decode(data: NSData) -> CacheType? {
1920
let image = NSImage(data: data)
@@ -22,6 +23,7 @@ extension NSImage: Cachable {
2223

2324
/**
2425
Encodes UIImage to NSData
26+
- Returns: Optional NSData
2527
*/
2628
public func encode() -> NSData? {
2729
guard let data = TIFFRepresentation else { return nil }
@@ -45,7 +47,7 @@ extension NSImage {
4547
Checks if image has alpha component
4648
*/
4749
var hasAlpha: Bool {
48-
var imageRect:CGRect = CGRectMake(0, 0, size.width, size.height)
50+
var imageRect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
4951
let imageRef = CGImageForProposedRect(&imageRect, context: nil, hints: nil)
5052
let result: Bool
5153
let alpha = CGImageGetAlphaInfo(imageRef)

Source/Shared/BasicHybridCache.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class BasicHybridCache: NSObject {
2828
self.name = name
2929
self.config = config
3030

31-
frontStorage = StorageFactory.resolve(name, kind: config.frontKind, maxSize: config.maxSize)
31+
frontStorage = StorageFactory.resolve(name, kind: config.frontKind, maxSize: UInt(config.maxObjects))
3232
backStorage = StorageFactory.resolve(name, kind: config.backKind, maxSize: config.maxSize)
3333

3434
super.init()

Source/Shared/Config.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ public struct Config {
1010
/// Expiry date that will be applied by default for every added object
1111
/// if it's not overridden in the add(key: object: expiry: completion:) method
1212
public let expiry: Expiry
13-
// Maximum size of the cache storage
13+
/// Maximum size of the cache storage
1414
public let maxSize: UInt
15+
/// Maximum amount of items to store in memory
16+
public let maxObjects: Int
1517

1618
// MARK: - Initialization
1719

@@ -22,12 +24,14 @@ public struct Config {
2224
- Parameter backKind: Back cache type
2325
- Parameter expiry: Expiry date that will be applied by default for every added object
2426
- Parameter maxSize: Maximum size of the cache storage
27+
- Parameter maxObjects: Maximum amount of objects to be stored in memory
2528
*/
26-
public init(frontKind: StorageKind, backKind: StorageKind, expiry: Expiry, maxSize: UInt) {
29+
public init(frontKind: StorageKind, backKind: StorageKind, expiry: Expiry = .Never, maxSize: UInt = 0, maxObjects: Int = 0) {
2730
self.frontKind = frontKind
2831
self.backKind = backKind
2932
self.expiry = expiry
3033
self.maxSize = maxSize
34+
self.maxObjects = maxObjects
3135
}
3236
}
3337

@@ -41,8 +45,6 @@ extension Config {
4145
public static var defaultConfig: Config {
4246
return Config(
4347
frontKind: .Memory,
44-
backKind: .Disk,
45-
expiry: .Never,
46-
maxSize: 0)
48+
backKind: .Disk)
4749
}
4850
}

Source/Shared/Extensions/JSON+Cache.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ extension JSON: Cachable {
1313
Creates JSON from NSData
1414

1515
- Parameter data: Data to decode from
16+
- Returns: An optional CacheType
1617
*/
1718
public static func decode(data: NSData) -> CacheType? {
1819
var result: CacheType?
1920

2021
do {
2122
let object = try NSJSONSerialization.JSONObjectWithData(data,
22-
options: NSJSONReadingOptions())
23+
options: NSJSONReadingOptions())
2324

24-
switch (object) {
25+
switch object {
2526
case let dictionary as [String : AnyObject]:
2627
result = JSON.Dictionary(dictionary)
2728
case let array as [AnyObject]:
@@ -36,6 +37,8 @@ extension JSON: Cachable {
3637

3738
/**
3839
Encodes JSON to NSData
40+
41+
- Returns: Optional NSData
3942
*/
4043
public func encode() -> NSData? {
4144
return try? NSJSONSerialization.dataWithJSONObject(object,

Source/Shared/Extensions/NSData+Cache.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ extension NSData: Cachable {
1313
Creates an instance from NSData
1414

1515
- Parameter data: Data to decode from
16+
- Returns: An optional CacheType
1617
*/
1718
public static func decode(data: NSData) -> CacheType? {
1819
return data
1920
}
2021

2122
/**
2223
Encodes an instance to NSData
24+
- Returns: Optional NSData
2325
*/
2426
public func encode() -> NSData? {
2527
return self

Source/Shared/Extensions/NSDate+Cache.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ extension NSDate: Cachable {
1313
Creates an instance from NSData
1414

1515
- Parameter data: Data to decode from
16+
- Returns: An optional CacheType
1617
*/
1718
public static func decode(data: NSData) -> CacheType? {
1819
return NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSDate
1920
}
2021

2122
/**
2223
Encodes an instance to NSData
24+
- Returns: Optional NSData
2325
*/
2426
public func encode() -> NSData? {
2527
return NSKeyedArchiver.archivedDataWithRootObject(self)

Source/Shared/Extensions/String+Cache.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extension String: Cachable {
1313
Creates a string from NSData
1414

1515
- Parameter data: Data to decode from
16+
- Returns: An optional CacheType
1617
*/
1718
public static func decode(data: NSData) -> CacheType? {
1819
guard let string = String(data: data, encoding: NSUTF8StringEncoding) else {
@@ -24,6 +25,7 @@ extension String: Cachable {
2425

2526
/**
2627
Encodes a string to NSData
28+
- Returns: Optional NSData
2729
*/
2830
public func encode() -> NSData? {
2931
return dataUsingEncoding(NSUTF8StringEncoding)
@@ -39,6 +41,8 @@ extension String {
3941

4042
/**
4143
Creates base64 string
44+
45+
- Returns: A base64 encoded string
4246
*/
4347
func base64() -> String {
4448
guard let data = self.dataUsingEncoding(NSUTF8StringEncoding) else { return self }

Source/Shared/Library/DefaultCacheConverter.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public struct DefaultCacheConverter<T> {
2323
Creates an instance from NSData
2424

2525
- Parameter data: Data to decode from
26+
- Returns: A generic type or throws
2627
*/
2728
public func decode(data: NSData) throws -> T {
2829
guard data.length == sizeof(T) else {
@@ -37,6 +38,9 @@ public struct DefaultCacheConverter<T> {
3738

3839
/**
3940
Encodes an instance to NSData
41+
42+
- Parameter value: A generic value
43+
- Returns: A NSData or throws
4044
*/
4145
public func encode(value: T) throws -> NSData {
4246
var value = value

0 commit comments

Comments
 (0)