ILIAS  trunk Revision v11.0_alpha-1811-gd2d5443e411
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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...
 

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'
 
Factory $data_factory
 
 $null_trafo
 
string $prefix_pattern
 

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

References null.

79  {
80  $this->data_factory = new Factory();
81  // see comment in buildFinalTransformation why this is not a good solution.
82  $this->null_trafo = new \ILIAS\Refinery\Custom\Transformation(fn($value): null => null);
83  $this->prefix_pattern = '(' . implode('|', [
84  preg_quote(self::STRING_PREFIX, '/'),
85  preg_quote(self::ARRAY_PREFIX, '/'),
86  preg_quote(self::INT_PREFIX, '/'),
87  preg_quote(self::BOOL_PREFIX, '/'),
88  preg_quote(self::NULL_PREFIX, '/')
89  ]) . ')';
90  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null

Member Function Documentation

◆ buildFinalTransformation()

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

Definition at line 177 of file ActiveContainer.php.

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

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

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

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

245  : void
246  {
247  if ($this->isLocked()) {
248  return;
249  }
250  $this->adaptor->delete($this->request->getContainerKey(), $key);
251  }
isLocked()
Returns true if the container is locked.
+ 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 253 of file ActiveContainer.php.

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

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

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

218  : string|int|array|bool|null
219  {
220  if ($this->isLocked()) {
221  return null;
222  }
223  $unpacked_values = $this->unpack(
224  $this->adaptor->get($this->request->getContainerKey(), $key)
225  );
226 
227  return $this->buildFinalTransformation($transformation)->transform($unpacked_values);
228  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
isLocked()
Returns true if the container is locked.
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 258 of file ActiveContainer.php.

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

◆ getContainerName()

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

Returns the name of the container.

Implements ILIAS\Cache\Container\Container.

Definition at line 263 of file ActiveContainer.php.

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

◆ getNullFallback()

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

Definition at line 186 of file ActiveContainer.php.

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

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

186  : Transformation
187  {
188  return $this->null_trafo;
189  }
+ 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 209 of file ActiveContainer.php.

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

209  : bool
210  {
211  if ($this->isLocked()) {
212  return false;
213  }
214 
215  return $this->adaptor->has($this->request->getContainerKey(), $key);
216  }
isLocked()
Returns true if the container is locked.
+ Here is the call 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 191 of file ActiveContainer.php.

References null.

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

191  : bool
192  {
193  // see comment in buildFinalTransformation why this is not a good solution.
194  $lock_until = $this->adaptor->get($this->request->getContainerKey(), self::LOCK_UNTIL);
195  $lock_until = $lock_until === null ? null : (float) $lock_until;
196 
197  return $lock_until !== null && $lock_until > microtime(true);
198  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ 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 200 of file ActiveContainer.php.

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

◆ pack()

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

Definition at line 92 of file ActiveContainer.php.

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

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

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

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

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

117  : array
118  {
119  array_walk($value, function (&$item): void {
120  $item = is_array($item) ? $this->packRecursive($item) : $this->pack($item);
121  });
122  return $value;
123  }
+ 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 230 of file ActiveContainer.php.

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

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

◆ unpack()

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

Definition at line 135 of file ActiveContainer.php.

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

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

135  : string|int|array|bool|null
136  {
137  // simple detection
138  if ($value === null) {
139  return null;
140  }
141  if ($value === self::NULL_PREFIX . self::GLUE) {
142  return null;
143  }
144 
145  // type detection
146  [$type, $unprefixed_value] = $this->unprefix($value);
147 
148  switch ($type) {
149  case self::STRING_PREFIX:
150  return $unprefixed_value;
151  case self::BOOL_PREFIX:
152  return $unprefixed_value === self::TRUE;
153  case self::ARRAY_PREFIX:
154  $unprefixed_value = json_decode((string) $unprefixed_value, true, 512);
155  if (!is_array($unprefixed_value)) {
156  return null;
157  }
158 
159  return $this->unpackRecursive($unprefixed_value);
160  case self::INT_PREFIX:
161  return (int) $unprefixed_value;
162  default:
163  return null;
164  }
165 
166  return null;
167  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ 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 169 of file ActiveContainer.php.

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

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

169  : array
170  {
171  array_walk($value, function (&$item): void {
172  $item = is_array($item) ? $this->unpackRecursive($item) : $this->unpack($item);
173  });
174  return $value;
175  }
+ 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 125 of file ActiveContainer.php.

References null.

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

125  : array
126  {
127  $str = '/^' . $this->prefix_pattern . preg_quote(self::GLUE, '/') . '(.*)/is';
128  if (!preg_match($str, $value, $matches)) {
129  return [self::NULL_PREFIX, null];
130  }
131 
132  return [$matches[1], $matches[2]];
133  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the caller graph for this function:

Field Documentation

◆ $data_factory

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

Definition at line 71 of file ActiveContainer.php.

◆ $null_trafo

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.

◆ BOOL_PREFIX

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

Definition at line 57 of file ActiveContainer.php.

◆ FALSE

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

Definition at line 69 of file ActiveContainer.php.

◆ GLUE

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

Definition at line 41 of file ActiveContainer.php.

◆ INT_PREFIX

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

Definition at line 53 of file ActiveContainer.php.

◆ LOCK_UNTIL

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

Definition at line 37 of file ActiveContainer.php.

◆ NULL_PREFIX

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

Definition at line 61 of file ActiveContainer.php.

◆ STRING_PREFIX

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

Definition at line 45 of file ActiveContainer.php.

◆ TRUE

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

Definition at line 65 of file ActiveContainer.php.


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