4
4
5
5
namespace Psalm \Internal ;
6
6
7
- use Amp \Serialization \SerializationException ;
8
7
use Amp \Serialization \Serializer ;
9
- use Closure ;
10
8
use Psalm \Config ;
11
9
use Psalm \Internal \Provider \Providers ;
12
10
use RuntimeException ;
13
11
12
+ use function fclose ;
14
13
use function file_exists ;
15
14
use function file_put_contents ;
16
- use function filemtime ;
17
- use function gzdeflate ;
18
- use function gzinflate ;
19
- use function igbinary_serialize ;
20
- use function igbinary_unserialize ;
15
+ use function flock ;
16
+ use function fopen ;
17
+ use function hash ;
21
18
use function is_dir ;
22
19
use function is_readable ;
23
- use function is_writable ;
24
- use function lz4_compress ;
25
- use function lz4_uncompress ;
20
+ use function json_decode ;
26
21
use function mkdir ;
27
- use function serialize ;
22
+ use function stream_get_contents ;
28
23
use function unlink ;
29
- use function unserialize ;
30
24
31
25
use const DIRECTORY_SEPARATOR ;
32
26
use const LOCK_EX ;
27
+ use const LOCK_UN ;
33
28
34
29
/**
35
30
* @internal
36
- *
37
31
* @template T as array|object|string
38
32
*/
39
33
final class Cache
@@ -43,12 +37,12 @@ final class Cache
43
37
44
38
/** @var array<string, string> */
45
39
private array $ idx = [];
46
- /** @var array<string, string> */
40
+ /** @var array<string, ? string> */
47
41
private array $ newIdx = [];
48
42
/** @var array<string, T> */
49
43
private array $ cache = [];
50
44
51
- public function __construct (Config $ config , string $ subdir , mixed $ dependencies = null )
45
+ public function __construct (Config $ config , string $ subdir , array $ dependencies = [] )
52
46
{
53
47
$ this ->serializer = $ config ->getCacheSerializer ();
54
48
@@ -71,6 +65,7 @@ public function __construct(Config $config, string $subdir, mixed $dependencies
71
65
}
72
66
}
73
67
68
+ $ dependencies []= $ config ->computeHash ();
74
69
$ dependencies = $ this ->serializer ->serialize ($ dependencies );
75
70
76
71
$ idx = fopen ($ this ->dir .'idx ' , 'r ' );
@@ -82,7 +77,8 @@ public function __construct(Config $config, string $subdir, mixed $dependencies
82
77
if ($ deps === $ dependencies ) {
83
78
$ this ->idx = $ idx ;
84
79
}
85
- } catch (RuntimeException ) {}
80
+ } catch (RuntimeException ) {
81
+ }
86
82
flock ($ idx , LOCK_UN );
87
83
fclose ($ idx );
88
84
}
@@ -91,9 +87,7 @@ public function __construct(Config $config, string $subdir, mixed $dependencies
91
87
public function getItem (string $ key , string $ hash = '' ): array |object |string |null
92
88
{
93
89
if (isset ($ this ->idx [$ key ]) && $ this ->idx [$ key ] !== $ hash ) {
94
- unset($ this ->idx [$ key ]);
95
- unset($ this ->newIdx [$ key ]);
96
- unset($ this ->cache [$ key ]);
90
+ $ this ->deleteItem ($ key );
97
91
return null ;
98
92
} elseif (!isset ($ this ->idx [$ key ])) {
99
93
return null ;
@@ -105,7 +99,7 @@ public function getItem(string $key, string $hash = ''): array|object|string|nul
105
99
106
100
$ path = $ this ->dir . DIRECTORY_SEPARATOR . hash ('xxh128 ' , $ key );
107
101
108
- if (!file_exists ($ path )
102
+ if (!file_exists ($ path )
109
103
|| !is_readable ($ path )
110
104
) {
111
105
return null ;
@@ -125,25 +119,31 @@ public function getItem(string $key, string $hash = ''): array|object|string|nul
125
119
126
120
public function deleteItem (string $ key ): void
127
121
{
128
- $ path = $ this ->dir . DIRECTORY_SEPARATOR . hash ('xxh128 ' , $ key );
129
- @unlink ($ path );
130
- unset($ this ->idx [$ key ]);
131
- unset($ this ->newIdx [$ key ]);
132
- unset($ this ->cache [$ key ]);
122
+ if (isset ($ this ->idx [$ key ])) {
123
+ $ path = $ this ->dir . DIRECTORY_SEPARATOR . hash ('xxh128 ' , $ key );
124
+ @unlink ($ path );
125
+ unset($ this ->idx [$ key ]);
126
+ unset($ this ->cache [$ key ]);
127
+ $ this ->newIdx [$ key ] = null ;
128
+ }
133
129
}
134
130
135
131
/** @param T $item */
136
132
public function saveItem (string $ key , array |object |string $ item , string $ hash = '' ): void
137
133
{
134
+ if (isset ($ this ->idx [$ key ]) && $ this ->idx [$ key ] === $ hash ) {
135
+ return ;
136
+ }
138
137
$ path = $ this ->dir . DIRECTORY_SEPARATOR . hash ('xxh128 ' , $ key );
139
138
file_put_contents ($ path , $ this ->serializer ->serialize ($ item ), LOCK_EX );
140
139
$ this ->cache [$ key ] = $ item ;
141
140
$ this ->idx [$ key ] = $ hash ;
142
141
$ this ->newIdx [$ key ] = $ hash ;
143
142
}
144
143
145
- /** @return array<string, string> */
146
- public function getNewIdx (): array {
144
+ /** @return array<string, ?string> */
145
+ public function getNewIdx (): array
146
+ {
147
147
return $ this ->newIdx ;
148
148
}
149
149
}
0 commit comments