DatabaseCacheService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 15
dl 0
loc 55
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A GetCachedItem() 0 8 2
A _LoadCacheObject() 0 2 1
A __construct() 0 8 1
A SetCachedItem() 0 5 1
A DeleteCachedItem() 0 2 1
1
<?php
2
namespace PhpDraft\Domain\Services;
3
4
use Silex\Application;
5
use Symfony\Component\HttpFoundation\Request;
6
use Phpfastcache\CacheManager;
7
8
//A wrapper service for the PHP-based caching to save on several MySQL reads
9
class DatabaseCacheService {
10
  private $cacheInstance;
11
12
  public function __construct(Application $app) {
13
    $this->app = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
14
15
    $cacheConfig = [
16
      'path' => CACHE_PATH
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\CACHE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
    ];
18
19
    $this->cacheInstance = CacheManager::Files($cacheConfig);
20
  }
21
22
  /*
23
  * Attempt to load a cached instance of an object.
24
  * @param string $itemKey The unique identifier of the object in the cache
25
  * @return object the cached object, or null if $itemKey was a miss in the cache
26
  */
27
  public function GetCachedItem($itemKey) {
28
    $cachedItem = $this->_LoadCacheObject($itemKey);
29
30
    if (!$cachedItem->isHit()) {
31
      return null;
32
    }
33
34
    return $cachedItem->get();
35
  }
36
37
  /*
38
  * Store an object in the cache for
39
  * @param string $itemKey The unique identifier of the object in the cache
40
  * @param object $item The object that is to be stored in the cache
41
  */
42
  public function SetCachedItem($itemKey, $item) {
43
    $cachedItem = $this->_LoadCacheObject($itemKey);
44
45
    $cachedItem->set($item)->expiresAfter(CACHE_SECONDS);
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\CACHE_SECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
46
    $this->cacheInstance->save($cachedItem);
47
  }
48
49
  /*
50
  * Remove an object from the cache
51
  * @param string $itemKey The unique identifier of the object in the cache
52
  */
53
  public function DeleteCachedItem($itemKey) {
54
    $this->cacheInstance->deleteItem($itemKey);
55
  }
56
57
  /*
58
  * Load a phpFastCache cache object for a given key. Is used in both get and set events.
59
  * @param string $itemKey The unique identifier of the object in the cache
60
  * @return object the phpFastCache object for the given key
61
  */
62
  private function _LoadCacheObject($itemKey) {
63
    return $this->cacheInstance->getItem($itemKey);
64
  }
65
}
66