Wiki source code of #[RPC\Cache]

Last modified by Ashterix on 2024/05/19 21:20

Show last authors
1 {{box cssClass="floatinginfobox"}}
2 == (% style="display:block; margin-top:-30px; text-align:center" %)Summary(%%) ==
3
4 (% style="margin-right:auto" %)
5 |**Classname**|(% colspan="2" rowspan="1" %)Cache
6 |**Namespace**|(% colspan="2" %)Ufo\RpcObject\RPC
7 |**Target**|(% colspan="2" rowspan="1" %)method
8 |(% colspan="3" %)**Arguments:**
9 |(% colspan="1" rowspan="3" %)**$lifetimeSecond**|**type**|int
10 |**optional**|true
11 |**default**|60
12 |(% colspan="1" rowspan="3" %)**$environments**|**type**|array
13 |**optional**|true
14 |**default**|['prod']
15 {{/box}}
16
17 = Method Response Caching =
18
19 By default, caching responses of your RPC methods is disabled, and each time you call the API, the method will be invoked.
20
21 However, sometimes, for certain methods whose results do not change often, it makes sense to enable response caching to increase request processing speed, save limits, reduce load on system components, etc.
22
23 The RPC Server uses the same CacheInterface implementation from Symfony that is used in your project. Therefore, there is no need for additional configuration.
24
25 Enabling caching is extremely simple; all you need to do is to add the attribute {{code language="none"}}#[RPC\Cache]{{/code}} to a particular method.
26
27 {{code language="php" layout="LINENUMBERS"}}
28 <?php
29 namespace App\Api\Procedures;
30
31 use Ufo\RpcObject\RPC;
32
33 class SomeApiMethod implements IRpcService
34 {
35 #[RPC\Cache]
36 public function methodThatWorksForALongTime(): void {}
37 }
38 {{/code}}
39
40 This setting will allow caching the response for one minute; subsequent requests to this method with the same parameters will receive the response instantly from the cache.
41
42 You can clear the cache as usual by executing the Symfony command:
43
44 {{code language="bash"}}
45 bin/console cache:clear
46 {{/code}}
47
48 If needed, you can increase the cache lifetime by passing the number of seconds in the parameter lifetimeSecond. For convenience, constants are created that cover most needs, but you can also pass the value as a simple number.
49
50 {{code language="php"}}
51 <?php
52 use Ufo\RpcObject\RPC\Cache;
53
54 Cache::T_MINUTE; // 60
55 Cache::T_5_MINUTES; // 300
56 Cache::T_10_MINUTES; // 600
57 Cache::T_30_MINUTES; // 1800
58 Cache::T_HOUR; // 3600
59 Cache::T_2_HOURS; // 7200
60 Cache::T_5_HOURS; // 18000
61 Cache::T_10_HOURS; // 36000
62 Cache::T_DAY; // 86400
63 Cache::T_2_DAYS; // 172800
64 Cache::T_3_DAYS; // 259200
65 Cache::T_5_DAYS; // 432000
66 Cache::T_10_DAYS; // 864000
67 Cache::T_WEEK; // 604800
68 Cache::T_2_WEEKS; // 1209600
69 Cache::T_3_WEEKS; // 1814400
70 Cache::T_MONTH; // 2592000
71 Cache::T_2_MONTHS; // 5184000
72 Cache::T_3_MONTHS; // 7776000
73 Cache::T_HALF_YEAR; // 15724800
74 Cache::T_YEAR; // 31536000
75 {{/code}}
76
77 Also, you can change the environment settings on which caching can be enabled. By default, caching only works in the {{code language="none"}}prod{{/code}} environment. However, you can change this by passing an array of environment names in which you want to cache results. In the example below, the result will be cached for one day for the {{code language="none"}}prod{{/code}} and {{code language="none"}}dev{{/code}} environments.
78
79 {{code language="php" layout="LINENUMBERS"}}
80 <?php
81 namespace App\Api\Procedures;
82
83 use Ufo\RpcObject\RPC;
84
85 class SomeApiMethod implements IRpcService
86 {
87 #[RPC\Cache(lifetimeSecond: RPC\Cache::T_DAY, environments: ['dev', 'prod'])]
88 public function methodThatWorksForALongTime(): void {}
89 }
90 {{/code}}