@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhutilKeyValueCacheProfiler extends PhutilKeyValueCacheProxy {
4
5 private $profiler;
6 private $name;
7
8 public function setName($name) {
9 $this->name = $name;
10 return $this;
11 }
12
13 public function getName() {
14 return $this->name;
15 }
16
17 /**
18 * Set a profiler for cache operations.
19 *
20 * @param PhutilServiceProfiler $profiler Service profiler.
21 * @return $this
22 * @task kvimpl
23 */
24 public function setProfiler(PhutilServiceProfiler $profiler) {
25 $this->profiler = $profiler;
26 return $this;
27 }
28
29
30 /**
31 * Get the current profiler.
32 *
33 * @return PhutilServiceProfiler|null Profiler, or null if none is set.
34 * @task kvimpl
35 */
36 public function getProfiler() {
37 return $this->profiler;
38 }
39
40
41 public function getKeys(array $keys) {
42 $call_id = null;
43 if ($this->getProfiler()) {
44 $call_id = $this->getProfiler()->beginServiceCall(
45 array(
46 'type' => 'kvcache-get',
47 'name' => $this->getName(),
48 'keys' => $keys,
49 ));
50 }
51
52 $results = parent::getKeys($keys);
53
54 if ($call_id !== null) {
55 $this->getProfiler()->endServiceCall(
56 $call_id,
57 array(
58 'hits' => array_keys($results),
59 ));
60 }
61
62 return $results;
63 }
64
65
66 public function setKeys(array $keys, $ttl = null) {
67 $call_id = null;
68 if ($this->getProfiler()) {
69 $call_id = $this->getProfiler()->beginServiceCall(
70 array(
71 'type' => 'kvcache-set',
72 'name' => $this->getName(),
73 'keys' => array_keys($keys),
74 'ttl' => $ttl,
75 ));
76 }
77
78 $result = parent::setKeys($keys, $ttl);
79
80 if ($call_id !== null) {
81 $this->getProfiler()->endServiceCall($call_id, array());
82 }
83
84 return $result;
85 }
86
87
88 public function deleteKeys(array $keys) {
89 $call_id = null;
90 if ($this->getProfiler()) {
91 $call_id = $this->getProfiler()->beginServiceCall(
92 array(
93 'type' => 'kvcache-del',
94 'name' => $this->getName(),
95 'keys' => $keys,
96 ));
97 }
98
99 $result = parent::deleteKeys($keys);
100
101 if ($call_id !== null) {
102 $this->getProfiler()->endServiceCall($call_id, array());
103 }
104
105 return $result;
106 }
107
108}