ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 ()
 

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 31 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 49 of file ActiveContainer.php.

53  {
54  $this->data_factory = new \ILIAS\Data\Factory();
55  // see comment in buildFinalTransformation why this is not a good solution.
56  $this->null_trafo = new \ILIAS\Refinery\Custom\Transformation(function ($value) {
57  return null;
58  });
59  $this->prefix_pattern = '(' . implode('|', [
60  preg_quote(self::STRING_PREFIX, '/'),
61  preg_quote(self::ARRAY_PREFIX, '/'),
62  preg_quote(self::INT_PREFIX, '/'),
63  preg_quote(self::BOOL_PREFIX, '/'),
64  preg_quote(self::NULL_PREFIX, '/')
65  ]) . ')';
66  }

Member Function Documentation

◆ buildFinalTransformation()

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

Definition at line 161 of file ActiveContainer.php.

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

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

162  {
163  // This is a workaround for the fact that the ByTrying transformation cannot be created by
164  // $DIC->refinery()->byTrying() since we are in a hell of dependencies. E.g. we cant instantiate the
165  // caching service with $DIC->refinery() since the Refinery needs ilLanguage, but ilLanguage
166  // needs the caching service...
167  return new ByTrying([$transformation, $this->getNullFallback()], $this->data_factory);
168  }
+ 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 244 of file ActiveContainer.php.

References ILIAS\LTI\ToolProvider\$key, and ILIAS\Cache\Container\ActiveContainer\isLocked().

244  : void
245  {
246  if ($this->isLocked()) {
247  return;
248  }
249  $this->adaptor->delete($this->request->getContainerKey(), $key);
250  }
isLocked()
Returns true if the container is locked.
string $key
Consumer key/client ID value.
Definition: System.php:193
+ 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 252 of file ActiveContainer.php.

252  : void
253  {
254  $this->adaptor->flushContainer($this->request->getContainerKey());
255  }

◆ 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 213 of file ActiveContainer.php.

References ILIAS\LTI\ToolProvider\$key, ILIAS\Cache\Container\ActiveContainer\buildFinalTransformation(), ILIAS\Cache\Container\ActiveContainer\has(), ILIAS\Cache\Container\ActiveContainer\isLocked(), and ILIAS\Cache\Container\ActiveContainer\unpack().

213  : string|int|array|bool|null
214  {
215  if ($this->isLocked()) {
216  return null;
217  }
218  if (!$this->has($key)) {
219  return null;
220  }
221 
222  $unpacked_values = $this->unpack(
223  $this->adaptor->get($this->request->getContainerKey(), $key)
224  );
225 
226  return $this->buildFinalTransformation($transformation)->transform($unpacked_values);
227  }
has(string $key)
Returns true if the container contains a value for the given key.
isLocked()
Returns true if the container is locked.
string $key
Consumer key/client ID value.
Definition: System.php:193
buildFinalTransformation(Transformation $transformation)
+ 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 257 of file ActiveContainer.php.

257  : string
258  {
259  return $this->config->getAdaptorName();
260  }

◆ getContainerName()

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

Returns the name of the container.

Implements ILIAS\Cache\Container\Container.

Definition at line 262 of file ActiveContainer.php.

262  : string
263  {
264  return $this->request->getContainerKey();
265  }

◆ getNullFallback()

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

Definition at line 170 of file ActiveContainer.php.

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

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

170  : Transformation
171  {
172  return $this->null_trafo;
173  }
+ 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 204 of file ActiveContainer.php.

References ILIAS\LTI\ToolProvider\$key, and ILIAS\Cache\Container\ActiveContainer\isLocked().

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

204  : bool
205  {
206  if ($this->isLocked()) {
207  return false;
208  }
209 
210  return $this->adaptor->has($this->request->getContainerKey(), $key);
211  }
isLocked()
Returns true if the container is locked.
string $key
Consumer key/client ID value.
Definition: System.php:193
+ 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 175 of file ActiveContainer.php.

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

175  : bool
176  {
177  if ($this->lock_cache !== null && $this->lock_cache > microtime(true)) {
178  return true;
179  }
180 
181  if (!$this->adaptor->has($this->request->getContainerKey(), self::LOCK_UNTIL)) {
182  $this->lock_cache = null;
183  return false;
184  }
185 
186  // see comment in buildFinalTransformation why this is not a good solution.
187  $lock_until = $this->adaptor->get($this->request->getContainerKey(), self::LOCK_UNTIL);
188  $lock_until = $lock_until === null ? null : (float) $lock_until;
189 
190  $this->lock_cache = $lock_until;
191 
192  return $lock_until !== null && $lock_until > microtime(true);
193  }
+ 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

Implements ILIAS\Cache\Container\Container.

Definition at line 195 of file ActiveContainer.php.

195  : void
196  {
197  if ($seconds > 300.0 || $seconds < 0.0) {
198  throw new \InvalidArgumentException('Locking for more than 5 minutes is not allowed.');
199  }
200  $lock_until = (string) (microtime(true) + $seconds);
201  $this->adaptor->set($this->request->getContainerKey(), self::LOCK_UNTIL, $lock_until, 300);
202  }

◆ pack()

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

Definition at line 68 of file ActiveContainer.php.

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

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

68  : string
69  {
70  if (is_string($value)) {
71  return self::STRING_PREFIX . self::GLUE . $value;
72  }
73  if (is_array($value)) {
74  $value = $this->packRecursive($value);
75 
76  return self::ARRAY_PREFIX . self::GLUE . json_encode($value, JSON_THROW_ON_ERROR);
77  }
78  if (is_int($value)) {
79  return self::INT_PREFIX . self::GLUE . $value;
80  }
81  if (is_bool($value)) {
82  return self::BOOL_PREFIX . self::GLUE . ($value ? self::TRUE : self::FALSE);
83  }
84  if (is_null($value)) {
85  return self::NULL_PREFIX . self::GLUE;
86  }
87 
88  throw new \InvalidArgumentException(
89  'Only strings, integers and arrays containing those values are allowed, ' . gettype($value) . ' given.'
90  );
91  }
+ 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 93 of file ActiveContainer.php.

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

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

93  : array
94  {
95  array_walk($value, function (&$item): void {
96  if (is_array($item)) {
97  $item = $this->packRecursive($item);
98  } else {
99  $item = $this->pack($item);
100  }
101  });
102  return $value;
103  }
+ 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 229 of file ActiveContainer.php.

References ILIAS\LTI\ToolProvider\$key, ILIAS\Cache\Container\ActiveContainer\isLocked(), and ILIAS\Cache\Container\ActiveContainer\pack().

229  : void
230  {
231  if ($this->isLocked()) {
232  return;
233  }
234  $ttl = $ttl ?? $this->config->getDefaultTTL();
235 
236  $this->adaptor->set(
237  $this->request->getContainerKey(),
238  $key,
239  $this->pack($value),
240  $ttl
241  );
242  }
isLocked()
Returns true if the container is locked.
string $key
Consumer key/client ID value.
Definition: System.php:193
+ Here is the call graph for this function:

◆ unlock()

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

Definition at line 270 of file ActiveContainer.php.

270  : void
271  {
272  $this->adaptor->delete($this->request->getContainerKey(), self::LOCK_UNTIL);
273  $this->lock_cache = null;
274  }

◆ unpack()

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

Definition at line 115 of file ActiveContainer.php.

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

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

115  : string|int|array|bool|null
116  {
117  // simple detection
118  if ($value === null) {
119  return null;
120  }
121  if ($value === self::NULL_PREFIX . self::GLUE) {
122  return null;
123  }
124 
125  // type detection
126  [$type, $unprefixed_value] = $this->unprefix($value);
127 
128  switch ($type) {
129  case self::STRING_PREFIX:
130  return $unprefixed_value;
131  case self::BOOL_PREFIX:
132  return $unprefixed_value === self::TRUE;
133  case self::ARRAY_PREFIX:
134  $unprefixed_value = json_decode($unprefixed_value, true, 512);
135  if (!is_array($unprefixed_value)) {
136  return null;
137  }
138 
139  return $this->unpackRecursive($unprefixed_value);
140  case self::INT_PREFIX:
141  return (int) $unprefixed_value;
142  default:
143  return null;
144  }
145 
146  return null;
147  }
+ 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 149 of file ActiveContainer.php.

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

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

149  : array
150  {
151  array_walk($value, function (&$item): void {
152  if (is_array($item)) {
153  $item = $this->unpackRecursive($item);
154  } else {
155  $item = $this->unpack($item);
156  }
157  });
158  return $value;
159  }
+ 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 105 of file ActiveContainer.php.

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

105  : array
106  {
107  $str = '/^' . $this->prefix_pattern . preg_quote(self::GLUE, '/') . '(.*)/is';
108  if (!preg_match($str, $value, $matches)) {
109  return [self::NULL_PREFIX, null];
110  }
111 
112  return [$matches[1], $matches[2]];
113  }
+ 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 43 of file ActiveContainer.php.

◆ $lock_cache

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

Definition at line 47 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 45 of file ActiveContainer.php.

◆ ARRAY_PREFIX

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

Definition at line 36 of file ActiveContainer.php.

◆ BOOL_PREFIX

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

Definition at line 38 of file ActiveContainer.php.

◆ FALSE

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

Definition at line 41 of file ActiveContainer.php.

◆ GLUE

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

Definition at line 34 of file ActiveContainer.php.

◆ INT_PREFIX

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

Definition at line 37 of file ActiveContainer.php.

◆ LOCK_UNTIL

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

Definition at line 33 of file ActiveContainer.php.

◆ NULL_PREFIX

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

Definition at line 39 of file ActiveContainer.php.

◆ STRING_PREFIX

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

Definition at line 35 of file ActiveContainer.php.

◆ TRUE

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

Definition at line 40 of file ActiveContainer.php.


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