Completed
Branch feature/pre-split (4ff102)
by Anton
03:27
created

EntityMap::flushCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM;
8
9
use Spiral\Models\EntityInterface;
10
use Spiral\ORM\Exceptions\CacheException;
11
12
/**
13
 * Entity cache provides ability to access already retrieved entities from memory instead of
14
 * calling database. Attention, cache WILL BE isolated in a selection scope in order to prevent
15
 * data collision, ie:
16
 *
17
 * $user1 => $users->findOne();
18
 * $user2 => $user->findOne();
19
 *
20
 * assert($user1 !== $user2);
21
 */
22
final class EntityMap
23
{
24
    /**
25
     * @var EntityInterface[]
26
     */
27
    private $entities = [];
28
29
    /**
30
     * Maximum entity cache size. Null is unlimited.
31
     *
32
     * @var int|null
33
     */
34
    private $maxSize = null;
35
36
    /**
37
     * @param int|null $maxSize Set to null to make cache size unlimited.
38
     */
39
    public function __construct(int $maxSize = null)
40
    {
41
        $this->maxSize = $maxSize;
42
    }
43
44
    /**
45
     * Add Record to entity cache. Primary key value will be used as
46
     * identifier.
47
     *
48
     * Attention, existed entity will be replaced!
49
     *
50
     * @param string          $class
51
     * @param string          $identity
52
     * @param RecordInterface $entity
53
     * @param bool            $ignoreLimit Cache overflow will be ignored.
54
     *
55
     * @return RecordInterface Returns given entity.
56
     *
57
     * @throws CacheException When cache size exceeded.
58
     */
59
    public function remember(
60
        string $class,
61
        string $identity,
62
        RecordInterface $entity,
63
        $ignoreLimit = true
64
    ): RecordInterface {
65
        if (!$ignoreLimit && count($this->entities) > $this->maxSize) {
66
            throw new CacheException('Entity cache size exceeded');
67
        }
68
69
        return $this->entities["{$class}.{$identity}"] = $entity;
70
    }
71
72
    /**
73
     * Remove entity record from entity cache. Primary key value will be used as identifier.
74
     *
75
     * @param string $class
76
     * @param string $identity
77
     */
78
    public function forget(string $class, string $identity)
79
    {
80
        unset($this->entities["{$class}.{$identity}"]);
81
    }
82
83
    /**
84
     * Check if desired entity was already cached.
85
     *
86
     * @param string $class
87
     * @param string $identity
88
     *
89
     * @return bool
90
     */
91
    public function has(string $class, string $identity): bool
92
    {
93
        return isset($this->entities["{$class}.{$identity}"]);
94
    }
95
96
    /**
97
     * Fetch entity from cache.
98
     *
99
     * @param string $class
100
     * @param string $identity
101
     *
102
     * @return null|mixed
103
     */
104
    public function get(string $class, string $identity)
105
    {
106
        if (empty($this->entities["{$class}.{$identity}"])) {
107
            return null;
108
        }
109
110
        return $this->entities[$class . '.' . $identity];
111
    }
112
113
    /**
114
     * Flush content of entity cache.
115
     */
116
    public function flushCache()
117
    {
118
        $this->entities = [];
119
    }
120
121
    /**
122
     * Destructing.
123
     */
124
    public function __destruct()
125
    {
126
        $this->flushCache();
127
    }
128
}