1616use Symfony \Component \Cache \Exception \InvalidArgumentException ;
1717use Symfony \Component \Cache \LockRegistry ;
1818use Symfony \Contracts \Cache \CacheInterface ;
19+ use Symfony \Contracts \Cache \CacheTrait ;
1920use Symfony \Contracts \Cache \ItemInterface ;
2021
2122/**
2223 * @author Nicolas Grekas <p@tchwork.com>
2324 *
2425 * @internal
2526 */
26- trait GetTrait
27+ trait ContractsTrait
2728{
29+ use CacheTrait {
30+ doGet as private contractsGet;
31+ }
32+
2833 private $ callbackWrapper = array (LockRegistry::class, 'compute ' );
2934
3035 /**
@@ -42,47 +47,12 @@ public function setCallbackWrapper(callable $callbackWrapper): callable
4247 return $ previousWrapper ;
4348 }
4449
45- /**
46- * {@inheritdoc}
47- */
48- public function get (string $ key , callable $ callback , float $ beta = null )
49- {
50- return $ this ->doGet ($ this , $ key , $ callback , $ beta );
51- }
52-
5350 private function doGet (AdapterInterface $ pool , string $ key , callable $ callback , ?float $ beta )
5451 {
5552 if (0 > $ beta = $ beta ?? 1.0 ) {
5653 throw new InvalidArgumentException (sprintf ('Argument "$beta" provided to "%s::get()" must be a positive number, %f given. ' , \get_class ($ this ), $ beta ));
5754 }
5855
59- $ t = 0 ;
60- $ item = $ pool ->getItem ($ key );
61- $ recompute = !$ item ->isHit () || INF === $ beta ;
62-
63- if (0 < $ beta ) {
64- if ($ recompute ) {
65- $ t = microtime (true );
66- } else {
67- $ metadata = $ item ->getMetadata ();
68- $ expiry = $ metadata [ItemInterface::METADATA_EXPIRY ] ?? false ;
69- $ ctime = $ metadata [ItemInterface::METADATA_CTIME ] ?? false ;
70-
71- if ($ ctime && $ expiry ) {
72- $ t = microtime (true );
73- $ recompute = $ expiry <= $ t - $ ctime / 1000 * $ beta * log (random_int (1 , PHP_INT_MAX ) / PHP_INT_MAX );
74- }
75- }
76- if ($ recompute ) {
77- // force applying defaultLifetime to expiry
78- $ item ->expiresAt (null );
79- }
80- }
81-
82- if (!$ recompute ) {
83- return $ item ->get ();
84- }
85-
8656 static $ save ;
8757
8858 $ save = $ save ?? \Closure::bind (
@@ -99,16 +69,19 @@ function (AdapterInterface $pool, ItemInterface $item, $value, float $startTime)
9969 CacheItem::class
10070 );
10171
102- // don't wrap nor save recursive calls
103- if (null === $ callbackWrapper = $ this ->callbackWrapper ) {
104- return $ callback ($ item );
105- }
106- $ this ->callbackWrapper = null ;
72+ return $ this ->contractsGet ($ pool , $ key , function (CacheItem $ item ) use ($ pool , $ callback , $ save ) {
73+ // don't wrap nor save recursive calls
74+ if (null === $ callbackWrapper = $ this ->callbackWrapper ) {
75+ return $ callback ($ item );
76+ }
77+ $ this ->callbackWrapper = null ;
78+ $ t = microtime (true );
10779
108- try {
109- return $ save ($ pool , $ item , $ callbackWrapper ($ item , $ callback , $ pool ), $ t );
110- } finally {
111- $ this ->callbackWrapper = $ callbackWrapper ;
112- }
80+ try {
81+ return $ save ($ pool , $ item , $ callbackWrapper ($ item , $ callback , $ pool ), $ t );
82+ } finally {
83+ $ this ->callbackWrapper = $ callbackWrapper ;
84+ }
85+ }, $ beta );
11386 }
11487}
0 commit comments