Skip to content

Commit

Permalink
Update cache option so folder is created
Browse files Browse the repository at this point in the history
  • Loading branch information
m1 committed Dec 21, 2015
1 parent 6dfe174 commit ec6a047
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,14 @@ getenv('test_key_1.test_key_2'); // value

Vars automatically caches the resources for 5 minutes, you can turn this off by setting the `cache` option to `false`.

The `cache_path` if not set is set to what the `base_path` is set to.
The `cache_path` if not set is set to what the `base_path` is set to. The `cache_path` must be writeable.

To invalidate the cache, simply just remove the folder inside your `cache_path` called `vars`, eg: `rm -rf /var/www/application/app/cache/vars`

The cache file is a .php file due to the extra speedup of opcache.

If you're using the Silex provider, then the cache will not be used and set if you're in debug mode.

#### Loaders

The loaders are what enable Vars to read the different file types (defaults are Ini, Json, Php, Toml, Xml and Yaml).
Expand Down
11 changes: 8 additions & 3 deletions src/Cache/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function __construct($resource, $options)
public function checkCache()
{
if ($this->provide && $this->path && !$this->getAttempted()) {
$file = sprintf('%s/%s.php', $this->path, $this->name);
$file = sprintf('%s/vars/%s.php', $this->path, $this->name);
$this->attempted = true;

if (is_file($file) &&
Expand All @@ -122,7 +122,7 @@ public function checkCache()
*/
public function load()
{
$cached_file = sprintf('%s/%s.php', $this->path, $this->name);
$cached_file = sprintf('%s/vars/%s.php', $this->path, $this->name);
$this->loaded_vars = unserialize(file_get_contents($cached_file));
}

Expand All @@ -134,7 +134,12 @@ public function load()
public function makeCache(Vars $vars)
{
if ($this->provide) {
$cache_file = sprintf('%s/%s.php', $this->path, $this->name);
$cache_folder = sprintf("%s/vars", $this->path);
if (!file_exists($cache_folder)) {
mkdir($cache_folder, 0777, true);
}

$cache_file = sprintf('%s/%s.php', $cache_folder, $this->name);
file_put_contents($cache_file, serialize($vars));
}
}
Expand Down
18 changes: 9 additions & 9 deletions tests/VarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public function testSetOptions()
$this->assertEquals($cache_path, $cache->getPath());
$this->assertEquals($cache_expire, $cache->getExpire());

unlink(sprintf('%s/%s', $cache_path, $cache_name));
unlink(sprintf('%s/vars/%s', $cache_path, $cache_name));
}

public function testSetBasePath()
Expand All @@ -624,7 +624,7 @@ public function testSetBasePath()
$this->assertEquals($path, $vars->getPath());
$this->assertEquals($path, $cache->getPath());

unlink(sprintf('%s/%s', $path, $cache_name));
unlink(sprintf('%s/vars/%s', $path, $cache_name));
}

public function testVariablesSet()
Expand Down Expand Up @@ -754,7 +754,7 @@ public function testBasicCache()
$this->assertEquals($output, $vars->getContent());
$this->assertEquals($cache_time, $cache->getTime());

unlink(sprintf('%s/%s', $cache_path, $cache_name));
unlink(sprintf('%s/vars/%s', $cache_path, $cache_name));
}

public function testCacheCheckInResourceProvider()
Expand Down Expand Up @@ -796,7 +796,7 @@ public function testCacheCheckInResourceProvider()
$this->assertEquals($output, $vars->getContent());
$this->assertEquals($cache_time, $cache->getTime());

unlink(sprintf('%s/%s', $cache_path, $cache_name));
unlink(sprintf('%s/vars/%s', $cache_path, $cache_name));
}

public function testCacheIsCreated()
Expand All @@ -813,9 +813,9 @@ public function testCacheIsCreated()
)
);

$this->assertTrue(is_file(sprintf('%s/%s', $cache_path, $cache_name)));
$this->assertTrue(is_file(sprintf('%s/vars/%s', $cache_path, $cache_name)));

unlink(sprintf('%s/%s', $cache_path, $cache_name));
unlink(sprintf('%s/vars/%s', $cache_path, $cache_name));
}

public function testCachePathIsSet()
Expand All @@ -830,10 +830,10 @@ public function testCachePathIsSet()
)
);

$this->assertTrue(is_file(sprintf('%s/%s', $cache_path, $cache_name)));
$this->assertTrue(is_file(sprintf('%s/vars/%s', $cache_path, $cache_name)));
$this->assertEquals($cache_path, $vars->getCache()->getPath());

unlink(sprintf('%s/%s', $cache_path, $cache_name));
unlink(sprintf('%s/vars/%s', $cache_path, $cache_name));
}
public function testGetResourceContent()
{
Expand Down Expand Up @@ -1048,7 +1048,7 @@ public function testOptionsSilexServiceProvider()
$this->assertEquals($cache_path, $cache->getPath());
$this->assertEquals($cache_expire, $cache->getExpire());

unlink(sprintf('%s/%s', $cache_path, $cache_name));
unlink(sprintf('%s/vars/%s', $cache_path, $cache_name));
}

public function testDebugSilexServiceProvider()
Expand Down

0 comments on commit ec6a047

Please sign in to comment.