Skip to content
Snippets Groups Projects

Collect the set cache events and size of the cached objects

3 files
+ 120
12
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 102
9
@@ -15,7 +15,9 @@ use Symfony\Component\HttpFoundation\RequestStack;
*/
class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidatorInterface {
const EVENT_NAME = 'CacheGet';
const GET_EVENT_NAME = 'CacheGet';
const SET_EVENT_NAME = 'CacheSet';
/**
* The wrapped cache backend.
@@ -45,6 +47,16 @@ class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidator
*/
protected $currentUser;
/**
* @var bool
*/
protected $isLoggingSizeEnabled;
/**
* @var bool
*/
protected $isLoggingWritesEnabled;
/**
* Constructs a new CacheBackendWrapper.
*
@@ -57,11 +69,13 @@ class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidator
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack service.
*/
public function __construct(CacheBackendInterface $cacheBackend, $bin, AccountProxyInterface $currentUser, RequestStack $requestStack) {
public function __construct(CacheBackendInterface $cacheBackend, $bin, AccountProxyInterface $currentUser, RequestStack $requestStack, bool $isLoggingSizeEnabled, bool $isLoggingWritesEnabled) {
$this->cacheBackend = $cacheBackend;
$this->bin = $bin;
$this->currentUser = $currentUser;
$this->requestStack = $requestStack;
$this->isLoggingSizeEnabled = $isLoggingSizeEnabled;
$this->isLoggingWritesEnabled = $isLoggingWritesEnabled;
}
/**
@@ -91,7 +105,12 @@ class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidator
'cf_ray' => $request ? $request->headers->get('CF-RAY') : NULL,
'username' => $this->currentUser->getAccountName(),
];
$this->record($attributes);
if ($this->isLoggingSizeEnabled) {
$attributes['size'] = $cache ? strlen(serialize($cache->data)) : NULL;
}
$this->recordGet($attributes);
return $cache;
}
@@ -126,7 +145,14 @@ class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidator
'cf_ray' => $request ? $request->headers->get('CF-RAY') : NULL,
'username' => $this->currentUser->getAccountName(),
];
$this->record($attributes);
if ($this->isLoggingSizeEnabled && isset($cache[$cid]) && isset($cache[$cid]->data)) {
if (!empty($cache[$cid]->data)) {
$attributes['size'] = strlen(serialize($cache[$cid]->data));
}
}
$this->recordGet($attributes);
}
return $cache;
@@ -136,14 +162,72 @@ class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidator
* {@inheritdoc}
*/
public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
return $this->cacheBackend->set($cid, $data, $expire, $tags);
$this->cacheBackend->set($cid, $data, $expire, $tags);
if (!$this->isLoggingWritesEnabled) {
return;
}
$request = $this->requestStack->getCurrentRequest();
$request_id = getenv('HTTP_X_REQUEST_ID');
$attributes = [
'duration' => NULL,
'cid' => $cid,
'bin' => $this->bin,
'expire' => $expire,
'tags' => implode(' ', $tags),
'isMultiple' => FALSE,
'uri' => $request ? $request->getBaseUrl() . $request->getPathInfo() : NULL,
// Acquia uses this to identify a request. https://docs.acquia.com/acquia-cloud/develop/env-variable/
'request_id' => $request_id ? $request_id : NULL,
// A Cloudflare trace header.
'cf_ray' => $request ? $request->headers->get('CF-RAY') : NULL,
'username' => $this->currentUser->getAccountName(),
];
if ($this->isLoggingSizeEnabled) {
$attributes['size'] = strlen(serialize($data));
}
$this->recordSet($attributes);
}
/**
* {@inheritdoc}
*/
public function setMultiple(array $items) {
return $this->cacheBackend->setMultiple($items);
$this->cacheBackend->setMultiple($items);
if (!$this->isLoggingWritesEnabled) {
return;
}
$request = $this->requestStack->getCurrentRequest();
$request_id = getenv('HTTP_X_REQUEST_ID');
foreach ($items as $cid => $item) {
$attributes = [
// Not possible to measure duration for getMultiple().
'duration' => NULL,
'cid' => $cid,
'bin' => $this->bin,
'expire' => isset($item['expire']) ? $item['expire'] : NULL,
'tags' => implode(' ', $item['tags']),
'isMultiple' => TRUE,
'uri' => $request ? $request->getBaseUrl() . $request->getPathInfo() : NULL,
// Acquia https://docs.acquia.com/acquia-cloud/develop/env-variable.
'request_id' => $request_id ? $request_id : NULL,
// A Cloudflare header.
'cf_ray' => $request ? $request->headers->get('CF-RAY') : NULL,
'username' => $this->currentUser->getAccountName(),
];
if ($this->isLoggingSizeEnabled) {
$attributes['size'] = strlen(serialize($item['data']));
}
$this->recordSet($attributes);
}
}
/**
@@ -212,13 +296,22 @@ class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidator
}
/**
* Record the event.
* Record the get event.
*
* @param array $attributes
* The array of attributes values.
*/
protected function record(array $attributes) {
newrelic_record_custom_event(self::EVENT_NAME, $attributes);
protected function recordGet(array $attributes) {
newrelic_record_custom_event(self::GET_EVENT_NAME, $attributes);
}
/**
* Record the set event.
*
* @param array $attributes
*/
protected function recordSet(array $attributes) {
newrelic_record_custom_event(self::SET_EVENT_NAME, $attributes);
}
}
Loading