ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ILIAS\Cache\Container\ActiveContainer Class Reference
+ Inheritance diagram for ILIAS\Cache\Container\ActiveContainer:
+ Collaboration diagram for ILIAS\Cache\Container\ActiveContainer:

Public Member Functions

 __construct (private Request $request, private Adaptor $adaptor, private Config $config)
 
 isLocked ()
 Returns true if the container is locked. More...
 
 lock (float $seconds)
 Locks the container for a given amount of seconds (max 300), in this time, get() will return null and has() will return false. More...
 
 has (string $key)
 Returns true if the container contains a value for the given key. More...
 
 get (string $key, Transformation $transformation)
 Returns the value for the given key, or null if the key does not exist. More...
 
 set (string $key, string|int|array|bool|null $value, ?int $ttl=null)
 Sets the value for the given key. More...
 
 delete (string $key)
 Deletes the value for the given key. More...
 
 flush ()
 Deletes all values in the container. More...
 
 getAdaptorName ()
 Returns the name of the adaptop used (such as apc, memcache, phpstatic) More...
 
 getContainerName ()
 Returns the name of the container. More...
 
 unlock ()
 
 lock (float $seconds)
 Locks the container for a given amount of seconds (max 300), in this time, get() will return null and has() will return false. More...
 
 isLocked ()
 Returns true if the container is locked. More...
 
 has (string $key)
 Returns true if the container contains a value for the given key. More...
 
 get (string $key, Transformation $transformation)
 Returns the value for the given key, or null if the key does not exist. More...
 
 set (string $key, string|int|array|bool|null $value, ?int $ttl=null)
 Sets the value for the given key. More...
 
 delete (string $key)
 Deletes the value for the given key. More...
 
 flush ()
 Deletes all values in the container. More...
 
 getAdaptorName ()
 Returns the name of the adaptop used (such as apc, memcache, phpstatic) More...
 
 getContainerName ()
 Returns the name of the container. More...
 

Protected Member Functions

 buildFinalTransformation (Transformation $transformation)
 
 getNullFallback ()
 

Private Member Functions

 pack (mixed $value)
 
 packRecursive (array $value)
 
 unprefix (string $value)
 
 unpack (?string $value)
 
 unpackRecursive (array $value)
 

Private Attributes

const LOCK_UNTIL = '_lock_until'
 
const GLUE = '|||||'
 
const STRING_PREFIX = 'string'
 
const ARRAY_PREFIX = 'array'
 
const INT_PREFIX = 'int'
 
const BOOL_PREFIX = 'bool'
 
const NULL_PREFIX = 'null'
 
const TRUE = 'true'
 
const FALSE = 'false'
 
ILIAS Data Factory $data_factory
 
Transformation $null_trafo
 
string $prefix_pattern
 
float $lock_cache = null
 

Detailed Description

Author
Fabian Schmid fabia.nosp@m.n@sr.nosp@m..solu.nosp@m.tion.nosp@m.s

Definition at line 32 of file ActiveContainer.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Cache\Container\ActiveContainer::__construct ( private Request  $request,
private Adaptor  $adaptor,
private Config  $config 
)

Definition at line 77 of file ActiveContainer.php.

81 {
82 $this->data_factory = new Factory();
83 // see comment in buildFinalTransformation why this is not a good solution.
84 $this->null_trafo = new \ILIAS\Refinery\Custom\Transformation(fn($value): null => null);
85 $this->prefix_pattern = '(' . implode('|', [
86 preg_quote(self::STRING_PREFIX, '/'),
87 preg_quote(self::ARRAY_PREFIX, '/'),
88 preg_quote(self::INT_PREFIX, '/'),
89 preg_quote(self::BOOL_PREFIX, '/'),
90 preg_quote(self::NULL_PREFIX, '/')
91 ]) . ')';
92 }
Builds data types.
Definition: Factory.php:36

Member Function Documentation

◆ buildFinalTransformation()

ILIAS\Cache\Container\ActiveContainer::buildFinalTransformation ( Transformation  $transformation)
protected

Definition at line 179 of file ActiveContainer.php.

179 : ByTrying
180 {
181 // This is a workaround for the fact that the ByTrying transformation cannot be created by
182 // $DIC->refinery()->byTrying() since we are in a hell of dependencies. E.g. we cant instantiate the
183 // caching service with $DIC->refinery() since the Refinery needs ilLanguage, but ilLanguage
184 // needs the caching service...
185 return new ByTrying([$transformation, $this->getNullFallback()], $this->data_factory);
186 }

References ILIAS\Cache\Container\ActiveContainer\getNullFallback().

Referenced by ILIAS\Cache\Container\ActiveContainer\get().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ delete()

ILIAS\Cache\Container\ActiveContainer::delete ( string  $key)

Deletes the value for the given key.

Implements ILIAS\Cache\Container\Container.

Definition at line 262 of file ActiveContainer.php.

262 : void
263 {
264 if ($this->isLocked()) {
265 return;
266 }
267 $this->adaptor->delete($this->request->getContainerKey(), $key);
268 }
isLocked()
Returns true if the container is locked.

References ILIAS\Cache\Container\ActiveContainer\isLocked().

+ Here is the call graph for this function:

◆ flush()

ILIAS\Cache\Container\ActiveContainer::flush ( )

Deletes all values in the container.

Implements ILIAS\Cache\Container\Container.

Definition at line 270 of file ActiveContainer.php.

270 : void
271 {
272 $this->adaptor->flushContainer($this->request->getContainerKey());
273 }

◆ get()

ILIAS\Cache\Container\ActiveContainer::get ( string  $key,
Transformation  $transformation 
)

Returns the value for the given key, or null if the key does not exist.

Implements ILIAS\Cache\Container\Container.

Definition at line 231 of file ActiveContainer.php.

231 : string|int|array|bool|null
232 {
233 if ($this->isLocked()) {
234 return null;
235 }
236 if (!$this->has($key)) {
237 return null;
238 }
239
240 $unpacked_values = $this->unpack(
241 $this->adaptor->get($this->request->getContainerKey(), $key)
242 );
243
244 return $this->buildFinalTransformation($transformation)->transform($unpacked_values);
245 }
buildFinalTransformation(Transformation $transformation)
has(string $key)
Returns true if the container contains a value for the given key.

References ILIAS\Cache\Container\ActiveContainer\buildFinalTransformation(), ILIAS\Cache\Container\ActiveContainer\has(), ILIAS\Cache\Container\ActiveContainer\isLocked(), and ILIAS\Cache\Container\ActiveContainer\unpack().

+ Here is the call graph for this function:

◆ getAdaptorName()

ILIAS\Cache\Container\ActiveContainer::getAdaptorName ( )

Returns the name of the adaptop used (such as apc, memcache, phpstatic)

Implements ILIAS\Cache\Container\Container.

Definition at line 275 of file ActiveContainer.php.

275 : string
276 {
277 return $this->config->getAdaptorName();
278 }

◆ getContainerName()

ILIAS\Cache\Container\ActiveContainer::getContainerName ( )

Returns the name of the container.

Implements ILIAS\Cache\Container\Container.

Definition at line 280 of file ActiveContainer.php.

280 : string
281 {
282 return $this->request->getContainerKey();
283 }

◆ getNullFallback()

ILIAS\Cache\Container\ActiveContainer::getNullFallback ( )
protected

Definition at line 188 of file ActiveContainer.php.

189 {
190 return $this->null_trafo;
191 }
A transformation is a function from one datatype to another.

References ILIAS\Cache\Container\ActiveContainer\$null_trafo.

Referenced by ILIAS\Cache\Container\ActiveContainer\buildFinalTransformation().

+ Here is the caller graph for this function:

◆ has()

ILIAS\Cache\Container\ActiveContainer::has ( string  $key)

Returns true if the container contains a value for the given key.

Implements ILIAS\Cache\Container\Container.

Definition at line 222 of file ActiveContainer.php.

222 : bool
223 {
224 if ($this->isLocked()) {
225 return false;
226 }
227
228 return $this->adaptor->has($this->request->getContainerKey(), $key);
229 }

References ILIAS\Cache\Container\ActiveContainer\isLocked().

Referenced by ILIAS\Cache\Container\ActiveContainer\get().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isLocked()

ILIAS\Cache\Container\ActiveContainer::isLocked ( )

Returns true if the container is locked.

Implements ILIAS\Cache\Container\Container.

Definition at line 193 of file ActiveContainer.php.

193 : bool
194 {
195 if ($this->lock_cache !== null && $this->lock_cache > microtime(true)) {
196 return true;
197 }
198
199 if (!$this->adaptor->has($this->request->getContainerKey(), self::LOCK_UNTIL)) {
200 $this->lock_cache = null;
201 return false;
202 }
203
204 // see comment in buildFinalTransformation why this is not a good solution.
205 $lock_until = $this->adaptor->get($this->request->getContainerKey(), self::LOCK_UNTIL);
206 $lock_until = $lock_until === null ? null : (float) $lock_until;
207
208 $this->lock_cache = $lock_until;
209
210 return $lock_until !== null && $lock_until > microtime(true);
211 }

Referenced by ILIAS\Cache\Container\ActiveContainer\delete(), ILIAS\Cache\Container\ActiveContainer\get(), ILIAS\Cache\Container\ActiveContainer\has(), and ILIAS\Cache\Container\ActiveContainer\set().

+ Here is the caller graph for this function:

◆ lock()

ILIAS\Cache\Container\ActiveContainer::lock ( float  $seconds)

Locks the container for a given amount of seconds (max 300), in this time, get() will return null and has() will return false.

Exceptions

InvalidArgumentException if $seconds is greater than 300 or less than 0

Implements ILIAS\Cache\Container\Container.

Definition at line 213 of file ActiveContainer.php.

213 : void
214 {
215 if ($seconds > 300.0 || $seconds < 0.0) {
216 throw new \InvalidArgumentException('Locking for more than 5 minutes is not allowed.');
217 }
218 $lock_until = (string) (microtime(true) + $seconds);
219 $this->adaptor->set($this->request->getContainerKey(), self::LOCK_UNTIL, $lock_until, 300);
220 }

References ILIAS\Cache\Container\ActiveContainer\LOCK_UNTIL.

◆ pack()

ILIAS\Cache\Container\ActiveContainer::pack ( mixed  $value)
private

Definition at line 94 of file ActiveContainer.php.

94 : string
95 {
96 if (is_string($value)) {
97 return self::STRING_PREFIX . self::GLUE . $value;
98 }
99 if (is_array($value)) {
100 $value = $this->packRecursive($value);
101
102 return self::ARRAY_PREFIX . self::GLUE . json_encode($value, JSON_THROW_ON_ERROR);
103 }
104 if (is_int($value)) {
105 return self::INT_PREFIX . self::GLUE . $value;
106 }
107 if (is_bool($value)) {
108 return self::BOOL_PREFIX . self::GLUE . ($value ? self::TRUE : self::FALSE);
109 }
110 if (is_null($value)) {
111 return self::NULL_PREFIX . self::GLUE;
112 }
113
114 throw new \InvalidArgumentException(
115 'Only strings, integers and arrays containing those values are allowed, ' . gettype($value) . ' given.'
116 );
117 }

References ILIAS\Cache\Container\ActiveContainer\FALSE, ILIAS\Cache\Container\ActiveContainer\GLUE, ILIAS\Cache\Container\ActiveContainer\packRecursive(), and ILIAS\Cache\Container\ActiveContainer\TRUE.

Referenced by ILIAS\Cache\Container\ActiveContainer\packRecursive(), and ILIAS\Cache\Container\ActiveContainer\set().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ packRecursive()

ILIAS\Cache\Container\ActiveContainer::packRecursive ( array  $value)
private

Definition at line 119 of file ActiveContainer.php.

119 : array
120 {
121 array_walk($value, function (&$item): void {
122 $item = is_array($item) ? $this->packRecursive($item) : $this->pack($item);
123 });
124 return $value;
125 }

References ILIAS\Cache\Container\ActiveContainer\pack(), and ILIAS\Cache\Container\ActiveContainer\packRecursive().

Referenced by ILIAS\Cache\Container\ActiveContainer\pack(), and ILIAS\Cache\Container\ActiveContainer\packRecursive().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set()

ILIAS\Cache\Container\ActiveContainer::set ( string  $key,
string|int|array|bool|null  $value,
?int  $ttl = null 
)

Sets the value for the given key.

Implements ILIAS\Cache\Container\Container.

Definition at line 247 of file ActiveContainer.php.

247 : void
248 {
249 if ($this->isLocked()) {
250 return;
251 }
252 $ttl = $ttl ?? $this->config->getDefaultTTL();
253
254 $this->adaptor->set(
255 $this->request->getContainerKey(),
256 $key,
257 $this->pack($value),
258 $ttl
259 );
260 }

References ILIAS\Cache\Container\ActiveContainer\isLocked(), and ILIAS\Cache\Container\ActiveContainer\pack().

+ Here is the call graph for this function:

◆ unlock()

ILIAS\Cache\Container\ActiveContainer::unlock ( )

Definition at line 288 of file ActiveContainer.php.

288 : void
289 {
290 $this->adaptor->delete($this->request->getContainerKey(), self::LOCK_UNTIL);
291 $this->lock_cache = null;
292 }

◆ unpack()

ILIAS\Cache\Container\ActiveContainer::unpack ( ?string  $value)
private

Definition at line 137 of file ActiveContainer.php.

137 : string|int|array|bool|null
138 {
139 // simple detection
140 if ($value === null) {
141 return null;
142 }
143 if ($value === self::NULL_PREFIX . self::GLUE) {
144 return null;
145 }
146
147 // type detection
148 [$type, $unprefixed_value] = $this->unprefix($value);
149
150 switch ($type) {
152 return $unprefixed_value;
154 return $unprefixed_value === self::TRUE;
156 $unprefixed_value = json_decode((string) $unprefixed_value, true, 512);
157 if (!is_array($unprefixed_value)) {
158 return null;
159 }
160
161 return $this->unpackRecursive($unprefixed_value);
162 case self::INT_PREFIX:
163 return (int) $unprefixed_value;
164 default:
165 return null;
166 }
167
168 return null;
169 }

References ILIAS\Cache\Container\ActiveContainer\ARRAY_PREFIX, ILIAS\Cache\Container\ActiveContainer\BOOL_PREFIX, ILIAS\Cache\Container\ActiveContainer\INT_PREFIX, ILIAS\Cache\Container\ActiveContainer\STRING_PREFIX, ILIAS\Cache\Container\ActiveContainer\TRUE, ILIAS\Cache\Container\ActiveContainer\unpackRecursive(), and ILIAS\Cache\Container\ActiveContainer\unprefix().

Referenced by ILIAS\Cache\Container\ActiveContainer\get(), and ILIAS\Cache\Container\ActiveContainer\unpackRecursive().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unpackRecursive()

ILIAS\Cache\Container\ActiveContainer::unpackRecursive ( array  $value)
private

Definition at line 171 of file ActiveContainer.php.

171 : array
172 {
173 array_walk($value, function (&$item): void {
174 $item = is_array($item) ? $this->unpackRecursive($item) : $this->unpack($item);
175 });
176 return $value;
177 }

References ILIAS\Cache\Container\ActiveContainer\unpack(), and ILIAS\Cache\Container\ActiveContainer\unpackRecursive().

Referenced by ILIAS\Cache\Container\ActiveContainer\unpack(), and ILIAS\Cache\Container\ActiveContainer\unpackRecursive().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unprefix()

ILIAS\Cache\Container\ActiveContainer::unprefix ( string  $value)
private

Definition at line 127 of file ActiveContainer.php.

127 : array
128 {
129 $str = '/^' . $this->prefix_pattern . preg_quote(self::GLUE, '/') . '(.*)/is';
130 if (!preg_match($str, $value, $matches)) {
131 return [self::NULL_PREFIX, null];
132 }
133
134 return [$matches[1], $matches[2]];
135 }

References ILIAS\Cache\Container\ActiveContainer\NULL_PREFIX.

Referenced by ILIAS\Cache\Container\ActiveContainer\unpack().

+ Here is the caller graph for this function:

Field Documentation

◆ $data_factory

ILIAS Data Factory ILIAS\Cache\Container\ActiveContainer::$data_factory
private

Definition at line 71 of file ActiveContainer.php.

◆ $lock_cache

float ILIAS\Cache\Container\ActiveContainer::$lock_cache = null
private

Definition at line 75 of file ActiveContainer.php.

◆ $null_trafo

Transformation ILIAS\Cache\Container\ActiveContainer::$null_trafo
private

◆ $prefix_pattern

string ILIAS\Cache\Container\ActiveContainer::$prefix_pattern
private

Definition at line 73 of file ActiveContainer.php.

◆ ARRAY_PREFIX

const ILIAS\Cache\Container\ActiveContainer::ARRAY_PREFIX = 'array'
private

Definition at line 49 of file ActiveContainer.php.

Referenced by ILIAS\Cache\Container\ActiveContainer\unpack().

◆ BOOL_PREFIX

const ILIAS\Cache\Container\ActiveContainer::BOOL_PREFIX = 'bool'
private

Definition at line 57 of file ActiveContainer.php.

Referenced by ILIAS\Cache\Container\ActiveContainer\unpack().

◆ FALSE

const ILIAS\Cache\Container\ActiveContainer::FALSE = 'false'
private

Definition at line 69 of file ActiveContainer.php.

Referenced by ILIAS\Cache\Container\ActiveContainer\pack().

◆ GLUE

const ILIAS\Cache\Container\ActiveContainer::GLUE = '|||||'
private

Definition at line 41 of file ActiveContainer.php.

Referenced by ILIAS\Cache\Container\ActiveContainer\pack().

◆ INT_PREFIX

const ILIAS\Cache\Container\ActiveContainer::INT_PREFIX = 'int'
private

Definition at line 53 of file ActiveContainer.php.

Referenced by ILIAS\Cache\Container\ActiveContainer\unpack().

◆ LOCK_UNTIL

const ILIAS\Cache\Container\ActiveContainer::LOCK_UNTIL = '_lock_until'
private

Definition at line 37 of file ActiveContainer.php.

Referenced by ILIAS\Cache\Container\ActiveContainer\lock().

◆ NULL_PREFIX

const ILIAS\Cache\Container\ActiveContainer::NULL_PREFIX = 'null'
private

Definition at line 61 of file ActiveContainer.php.

Referenced by ILIAS\Cache\Container\ActiveContainer\unprefix().

◆ STRING_PREFIX

const ILIAS\Cache\Container\ActiveContainer::STRING_PREFIX = 'string'
private

Definition at line 45 of file ActiveContainer.php.

Referenced by ILIAS\Cache\Container\ActiveContainer\unpack().

◆ TRUE

const ILIAS\Cache\Container\ActiveContainer::TRUE = 'true'
private

The documentation for this class was generated from the following file: