-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from mtslzr/feature/cache_settings
Settings for Cache
- Loading branch information
Showing
6 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package pokeapi | ||
|
||
import "time" | ||
|
||
var CacheSettings = Settings{ | ||
CustomExpire: 0, | ||
UseCache: true, | ||
} | ||
|
||
var defaultCacheSettings = defaultSettings{ | ||
MaxExpire: 10 * time.Minute, | ||
MinExpire: 5 * time.Minute, | ||
} | ||
|
||
// defaultSettings are settings not meant to be changed. | ||
type defaultSettings struct { | ||
MaxExpire time.Duration | ||
MinExpire time.Duration | ||
} | ||
|
||
// CacheSettings are user settings for cache expiration. | ||
type Settings struct { | ||
CustomExpire time.Duration | ||
UseCache bool | ||
} | ||
|
||
// ClearCache clears all cached data. | ||
func ClearCache() { | ||
c.Flush() | ||
} | ||
|
||
// setCache adds new item to local cache. | ||
func setCache(endpoint string, body []byte) { | ||
if CacheSettings.CustomExpire != 0 { | ||
c.Set(endpoint, body, CacheSettings.CustomExpire * time.Minute) | ||
} else { | ||
c.SetDefault(endpoint, body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters