Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
Require cache key to be a string, added test for cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
Wruczek committed Jan 20, 2018
1 parent 05ce386 commit 96fd502
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/PhpFileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@ class PhpFileCache {
* @param string $cacheDirPath cache directory. Must end with "/"
* @param string $cacheFileName cache file name
* @param string $cacheFileExtension cache file extension. Must end with .php
* @throws \Exception if there is a problem with loading the cache
* @throws \Exception if there is a problem loading the cache
*/
public function __construct($cacheDirPath = "cache/", $cacheFileName = "defaultcache", $cacheFileExtension = ".cache.php") {
$this->setCacheFilename($cacheFileName);
$this->setCacheDir($cacheDirPath);
$this->setCacheFileExtension($cacheFileExtension);
$this->setDevMode(false);

$this->reloadFromDisc();
}

/**
* Loads cache
* @return array array filled with data
* @throws \Exception if there is a problem with loading the cache
* @throws \Exception if there is a problem loading the cache
*/
private function loadCacheFile() {
$filepath = $this->getCacheFilePath();
Expand Down Expand Up @@ -131,6 +132,10 @@ private function saveCacheFile() {
* @throws \Exception if the file cannot be saved
*/
public function store($key, $data, $expiration = 60, $permanent = false) {
if(!is_string($key)) {
throw new \InvalidArgumentException('$key must be a string, got type "' . get_class($key) . '" instead');
}

if ($this->isDevMode())
$expiration = 1;

Expand Down Expand Up @@ -301,7 +306,7 @@ public function debugCache() {
/**
* Reloads cache from disc. Can be used after changing file name, extension or cache dir
* using functions instead of constructor. (This class loads data once, when is created)
* @throws \Exception if there is a problem with loading the cache
* @throws \Exception if there is a problem loading the cache
*/
public function reloadFromDisc() {
// Try to load the cache, otherwise create a empty array
Expand All @@ -312,8 +317,13 @@ public function reloadFromDisc() {
* Returns md5 hash of the given string.
* @param $str string String to be hashed
* @return string MD5 hash
* @throws \InvalidArgumentException if $str is not a string
*/
private function getStringHash($str) {
if(!is_string($str)) {
throw new \InvalidArgumentException('$key must be a string, got type "' . get_class($str) . '" instead');
}

return md5($str);
}

Expand Down Expand Up @@ -379,8 +389,13 @@ public function getCacheFilename() {
* Sets new cache file name. If you want to read data from new file, consider calling reloadFromDisc.
* @param string $cacheFilename
* @return $this
* @throws \InvalidArgumentException if $cacheFilename is not a string
*/
public function setCacheFilename($cacheFilename) {
if(!is_string($cacheFilename)) {
throw new \InvalidArgumentException('$key must be a string, got type "' . get_class($cacheFilename) . '" instead');
}

$this->cacheFilename = $cacheFilename;
$this->cacheFilenameHashed = $this->getStringHash($cacheFilename);
return $this;
Expand Down
8 changes: 8 additions & 0 deletions tests/PhpFileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public function testStore() {
self::assertFalse($cache->isExpired("test"));
}

public function testKeyCharacters() {
$cache = new PhpFileCache(self::__TESTDIR);

foreach (["'", "\"", "test & test", "óÓłć€$123"] as $key) {
self::assertSame($this->testarray, $cache->store($key, $this->testarray)->retrieve($key));
}
}

public function testRetrieve() {
$cache = new PhpFileCache(self::__TESTDIR);

Expand Down

0 comments on commit 96fd502

Please sign in to comment.