Skip to content

Commit

Permalink
Start using a FileCache, rather than being one.
Browse files Browse the repository at this point in the history
  • Loading branch information
netcarver committed Nov 26, 2017
1 parent 6ed4610 commit 314a865
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
3 changes: 2 additions & 1 deletion BitbucketRepositoryAdaptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public function __construct($http, $remote, $application, array $options) {
$this->debug($options['debug']);
}
if (array_key_exists('cache_dir', $options)) {
$this->setCacheDirectory($options['cache_dir']);
$this->cache = new \Netcarver\FileCache();
$this->cache->setCacheDirectory($options['cache_dir']);
} else {
throw new \Exception('Cache directory is needed.');
}
Expand Down
19 changes: 11 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# **Changelog** - [Keep a Changelog]

## [Upcoming]
### Added
- Sections to the config screen

### Changed
- Show more/Show all buttons to use current theme styles. Thanks @matjazp.

### Removed
- Semantic versioning from changelog - not quite ready for it yet.
- Repo access code into separate adaptor classes that implement the GitRepository Interface.
- Added Github interface.
- Added basic BitBucket interface (WIP).
- Added capabilities flags to the repo interface.
- Rework the Update screen code to use the new interfaces.
- Grouped config inputs into sections on the config screen.
- Changed the Show more/Show all buttons to use current theme styles. Thanks @matjazp.
- Remove Semantic versioning from changelog - not quite ready for it yet.
- Attempt to fix "prism cannot be unloaded" issue.
- Simplifiy changelog formatting.
- Add result caching with config settings.


## [0.9.2] - 2017-11-22
Expand Down
6 changes: 3 additions & 3 deletions FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setCacheDirectory($dir = null) {
/**
*
*/
protected function urlToKey($url) {
public function urlToKey($url) {
$p = parse_url($url);
$key = $p['host'] . str_replace('/', '-', $p['path']);
return $key;
Expand All @@ -51,7 +51,7 @@ protected function keyToStorageLocation($key) {
/**
*
*/
protected function getEntryForKey($key) {
public function getEntryForKey($key) {
$file = $this->keyToStorageLocation($key);
$entry = @file_get_contents($file);
if ($entry) {
Expand All @@ -69,7 +69,7 @@ protected function getEntryForKey($key) {
/**
*
*/
protected function setEntryForKey($key, $entry) {
public function setEntryForKey($key, $entry) {
$file = $this->keyToStorageLocation($key);
$entry = json_encode($entry);

Expand Down
16 changes: 8 additions & 8 deletions GitRepositoryAdaptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
/**
*
*/
class GitRepositoryAdaptor extends FileCache {
class GitRepositoryAdaptor {

// Per-instance data...
protected $cache = null;
protected $remote = null;
protected $owner = null;
protected $repo = null;
Expand Down Expand Up @@ -52,8 +53,8 @@ protected function RepositoryRead($url, &$http_code, $json = true) {
$etag = null;
$last_mod = null;
$headers = $this->headers;
$key = $this->urlToKey($url);
$entry = $this->getEntryForKey($key);
$key = $this->cache->urlToKey($url);
$entry = $this->cache->getEntryForKey($key);
if ($entry) {
$reply = $entry['reply'];
$etag = $entry['etag'];
Expand Down Expand Up @@ -95,7 +96,7 @@ protected function RepositoryRead($url, &$http_code, $json = true) {
$entry['etag'] = @$response_headers['etag'];
$entry['last_mod'] = @$response_headers['Last-Modified'];
$entry['reply'] = $new_reply;
$this->setEntryForKey($key, $entry);
$this->cache->setEntryForKey($key, $entry);
$reply = $new_reply;
break;

Expand Down Expand Up @@ -127,13 +128,12 @@ protected function RepositoryRead($url, &$http_code, $json = true) {
/**
* No such resource.
*/
$cache_for = 2;
$cache_for = 15;
$reply = null;
$entry['etag'] = '404';
$entry['last_mod'] = time() + ($cache_for * 60); // Cache 404s for 10 minutes TODO set to 600 before commit. NO COMMIT
$entry['last_mod'] = time() + ($cache_for * 60); // Cache 404s for n minutes
$entry['reply'] = null;
$this->setEntryForKey($key, $entry);
\TD::barDump(sprintf("Caching returned 404 for %u minutes.", $cache_for));
$this->cache->setEntryForKey($key, $entry);
break;
}

Expand Down
3 changes: 2 additions & 1 deletion GithubRepositoryAdaptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function __construct($http, $remote, $application, array $options) {
$this->debug($options['debug']);
}
if (array_key_exists('cache_dir', $options)) {
$this->setCacheDirectory($options['cache_dir']);
$this->cache = new \Netcarver\FileCache();
$this->cache->setCacheDirectory($options['cache_dir']);
} else {
throw new \Exception('Cache directory is needed.');
}
Expand Down

0 comments on commit 314a865

Please sign in to comment.