Base   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 10
c 1
b 0
f 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 3 1
A delete() 0 3 1
A get() 0 11 4
A cleanup() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2026
6
 * @package Base
7
 * @subpackage Cache
8
 */
9
10
11
namespace Aimeos\Base\Cache;
12
13
14
/**
15
 * Common class for all cache classes.
16
 *
17
 * @package Base
18
 * @subpackage Cache
19
 */
20
abstract class Base implements \Aimeos\Base\Cache\Iface
21
{
22
	/**
23
	 * Removes all expired cache entries.
24
	 *
25
	 * @return bool True on success and false on failure
26
	 */
27
	public function cleanup() : bool
28
	{
29
		return true;
30
	}
31
32
33
	/**
34
	 * Removes the cache entry identified by the given key.
35
	 *
36
	 * @param string $key Key string that identifies the single cache entry
37
	 * @return bool True if the item was successfully removed. False if there was an error
38
	 * @throws \Psr\SimpleCache\InvalidArgumentException
39
	 */
40
	public function delete( string $key ) : bool
41
	{
42
		return $this->deleteMultiple( [$key] );
43
	}
44
45
46
	/**
47
	 * Returns the cached value for the given key.
48
	 *
49
	 * @param string $key Path to the requested value like product/id/123
50
	 * @param mixed $default Value returned if requested key isn't found
51
	 * @return mixed Value associated to the requested key. If no value for the
52
	 *	key is found in the cache, the given default value is returned
53
	 * @throws \Psr\SimpleCache\InvalidArgumentException
54
	 */
55
	public function get( string $key, $default = null )
56
	{
57
		$list = $this->getMultiple( [$key] );
58
59
		if( $list instanceof \Iterator ) {
60
			return $list->current();
61
		} elseif( is_array( $list ) && ( $value = current( $list ) ) !== false ) {
62
			return $value;
63
		}
64
65
		return $default;
66
	}
67
68
69
	/**
70
	 * Sets the value for the given key in the cache.
71
	 *
72
	 * @param string $key Key string for the given value like product/id/123
73
	 * @param mixed $value Value string that should be stored for the given key
74
	 * @param \DateInterval|int|string|null $expires Date interval object,
75
	 *  date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value
76
	 *  when the cache entry will expiry
77
	 * @param iterable $tags List of tag strings that should be assoicated to the cache entry
78
	 * @return bool True on success and false on failure.
79
	 * @throws \Psr\SimpleCache\InvalidArgumentException
80
	 */
81
	public function set( string $key, $value, $expires = null, iterable $tags = [] ) : bool
82
	{
83
		return $this->setMultiple( [$key => $value], $expires, $tags );
84
	}
85
}
86