Skip to content

Commit 1db57c1

Browse files
Merge pull request #61 from dapr/update/actors
Add cache for actors
2 parents 1c6c047 + 807c851 commit 1db57c1

File tree

9 files changed

+366
-51
lines changed

9 files changed

+366
-51
lines changed

composer.lock

Lines changed: 34 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Dapr\Actors\Generators\ExistingOnly;
1010
use Dapr\Actors\Generators\FileGenerator;
1111
use Dapr\Actors\Generators\ProxyFactory;
12+
use Dapr\Actors\Internal\Caches\FileCache;
1213
use Dapr\App;
1314
use Dapr\DaprClient;
1415
use Dapr\Deserialization\DeserializationConfig;
@@ -130,6 +131,7 @@
130131
'dapr.actors.scan_interval' => null,
131132
'dapr.actors.drain_timeout' => null,
132133
'dapr.actors.drain_enabled' => null,
134+
'dapr.actors.cache' => FileCache::class,
133135
'dapr.http.middleware.request' => [get(Tracing::class)],
134136
'dapr.http.middleware.response' => [get(ApplicationJson::class), get(Tracing::class)],
135137
'dapr.port' => env('DAPR_HTTP_PORT', "3500"),

src/lib/Actors/ActorRuntime.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
namespace Dapr\Actors;
44

5+
use Dapr\Actors\Internal\Caches\CacheInterface;
6+
use Dapr\Actors\Internal\Caches\NoCache;
57
use Dapr\Deserialization\IDeserializer;
68
use Dapr\exceptions\DaprException;
79
use Dapr\exceptions\Http\NotFound;
810
use Dapr\exceptions\SaveStateFailure;
11+
use DI\Container;
912
use DI\DependencyException;
1013
use DI\FactoryInterface;
1114
use DI\NotFoundException;
1215
use Exception;
16+
use JetBrains\PhpStorm\ArrayShape;
1317
use Psr\Log\LoggerInterface;
1418
use ReflectionClass;
1519
use ReflectionException;
@@ -24,10 +28,14 @@
2428
*/
2529
class 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 $actor, string $dapr_type): void
6573
{
6674
$id = $actor->get_id();
75+
/**
76+
* @var $cache CacheInterface
77+
*/
78+
foreach ($this->caches[$dapr_type.$id] as $cache) {
79+
$cache->reset();
80+
}
6781

6882
$activation_tracker = hash('sha256', $dapr_type.$id);
6983
$activation_tracker = rtrim(
@@ -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_type = new ReflectionClass($type_name);
181196
if ($reflected_type->isSubclassOf(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_type, string $id, string $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 = NoCache::class;
224+
}
225+
$cache_name = $dapr_type.$id;
226+
$this->caches[$cache_name][$state_type] ??= new $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_transaction->invoke($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 $reflection, string $dapr_type, string $id, array $states): IActor
242282
{
243283
$states['id'] = $id;
244-
$actor = $this->container->make($reflection->getName(), $states);
284+
$actor = $this->factory->make($reflection->getName(), $states);
245285
$activation_tracker = hash('sha256', $dapr_type.$id);
246286
$activation_tracker = rtrim(
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.$id] as $cache) {
303+
$cache->reset();
304+
}
259305
$actor->on_activation();
260306
}
261307

0 commit comments

Comments
 (0)