22
33namespace  Dapr \Actors ;
44
5+ use  Dapr \Actors \Internal \Caches \CacheInterface ;
6+ use  Dapr \Actors \Internal \Caches \NoCache ;
57use  Dapr \Deserialization \IDeserializer ;
68use  Dapr \exceptions \DaprException ;
79use  Dapr \exceptions \Http \NotFound ;
810use  Dapr \exceptions \SaveStateFailure ;
11+ use  DI \Container ;
912use  DI \DependencyException ;
1013use  DI \FactoryInterface ;
1114use  DI \NotFoundException ;
1215use  Exception ;
16+ use  JetBrains \PhpStorm \ArrayShape ;
1317use  Psr \Log \LoggerInterface ;
1418use  ReflectionClass ;
1519use  ReflectionException ;
2428 */ 
2529class  ActorRuntime
2630{
31+  #[ArrayShape(['string '  => ['string '  => CacheInterface::class]])]
32+  private  array  $ caches
33+ 
2734 public  function  __construct (
2835 protected  LoggerInterface $ logger
2936 protected  ActorConfig $ actor_config
30-  protected  FactoryInterface $ container
37+  protected  FactoryInterface $ factory
38+  protected  Container $ container
3139 protected  IDeserializer $ deserializer
3240 ) {
3341 }
@@ -64,6 +72,12 @@ public function do_method(IActor $actor, string $method, mixed $arg): mixed
6472 public  function  deactivate_actor (IActor $ actorstring  $ dapr_typevoid 
6573 {
6674 $ id$ actorget_id ();
75+  /** 
76+  * @var $cache CacheInterface 
77+  */ 
78+  foreach  ($ this caches [$ dapr_type$ idas  $ cache
79+  $ cachereset ();
80+  }
6781
6882 $ activation_trackerhash ('sha256 ' , $ dapr_type$ id
6983 $ activation_trackerrtrim (
@@ -110,6 +124,7 @@ public function resolve_actor(string $dapr_type, string $id, callable $loan): mi
110124 } catch  (DependencyException DaprException NotFoundException $ e
111125 throw  new  SaveStateFailure ('Failed to commit actor state ' , previous: $ e
112126 }
127+ 
113128 // @codeCoverageIgnoreEnd 
114129
115130 return  $ result
@@ -179,6 +194,7 @@ protected function get_states(ReflectionClass $reflection, string $dapr_type, st
179194 if  (class_exists ($ type_name
180195 $ reflected_typenew  ReflectionClass ($ type_name
181196 if  ($ reflected_typeisSubclassOf (ActorState::class)) {
197+  $ this setup_cache ($ dapr_type$ id$ type_name
182198 $ state$ this container ->make ($ type_name
183199 $ this begin_transaction ($ state$ reflected_type$ dapr_type$ id
184200
@@ -188,10 +204,29 @@ protected function get_states(ReflectionClass $reflection, string $dapr_type, st
188204 }
189205 }
190206 }
207+  $ this teardown_cache ();
191208
192209 return  $ states
193210 }
194211
212+  /** 
213+  * @param string $dapr_type 
214+  * @param string $id 
215+  * @param string $state_type 
216+  */ 
217+  private  function  setup_cache (string  $ dapr_typestring  $ idstring  $ state_type
218+  {
219+  try  {
220+  $ cache_type$ this container ->get ('dapr.actors.cache ' );
221+  } catch  (DependencyException NotFoundException 
222+  $ this logger ->warning ('No cache type found, turning off actor state cache. Set `dapr.actors.cache` ' );
223+  $ cache_type
224+  }
225+  $ cache_name$ dapr_type$ id
226+  $ this caches [$ cache_name$ state_typenew  $ cache_type$ cache_name
227+  $ this container ->set (CacheInterface::class, $ this caches [$ cache_name$ state_type
228+  }
229+ 
195230 /** 
196231 * Begins an actor transaction 
197232 * 
@@ -226,6 +261,11 @@ protected function begin_transaction(
226261 $ begin_transactioninvoke ($ state$ dapr_type$ actor_id
227262 }
228263
264+  private  function  teardown_cache ()
265+  {
266+  $ this container ->set (CacheInterface::class, null );
267+  }
268+ 
229269 /** 
230270 * Instantiates an actor implementation 
231271 * 
@@ -241,7 +281,7 @@ protected function begin_transaction(
241281 protected  function  get_actor (ReflectionClass $ reflectionstring  $ dapr_typestring  $ idarray  $ statesIActor 
242282 {
243283 $ states'id ' ] = $ id
244-  $ actor$ this container ->make ($ reflectiongetName (), $ states
284+  $ actor$ this factory ->make ($ reflectiongetName (), $ states
245285 $ activation_trackerhash ('sha256 ' , $ dapr_type$ id
246286 $ activation_trackerrtrim (
247287 sys_get_temp_dir (),
@@ -256,6 +296,12 @@ protected function get_actor(ReflectionClass $reflection, string $dapr_type, str
256296 ['type '  => $ dapr_type'id '  => $ id
257297 );
258298 touch ($ activation_tracker
299+  /** 
300+  * @var $cache CacheInterface 
301+  */ 
302+  foreach  ($ this caches [$ dapr_type$ idas  $ cache
303+  $ cachereset ();
304+  }
259305 $ actoron_activation ();
260306 }
261307
0 commit comments