|
| 1 | +.. |
| 2 | + Copyright (c) 2022, 2024, Oracle and/or its affiliates. |
| 3 | + Licensed under the Universal Permissive License v 1.0 as shown at |
| 4 | + https://oss.oracle.com/licenses/upl. |
| 5 | +
|
| 6 | +Caches |
| 7 | +====== |
| 8 | + |
| 9 | +Once a Session has been obtained, it is now possible to begin working with |
| 10 | +`NamedMap` and/or `NamedCache` instances. This is done by calling either |
| 11 | +`Session.get_map(name: str, cache_options: Optional[CacheOptions] = None)` or |
| 12 | +`Session.get_cache(name: str, cache_options: Optional[CacheOptions] = None)`. |
| 13 | +The `name` argument is the logical name for this cache. The optional `cache_options` |
| 14 | +argument accepts a `CacheOptions` to configure the default time-to-live (ttl) |
| 15 | +for entries placed in the cache as well as allowing the configuration of near |
| 16 | +caching (discussed later in this section). |
| 17 | + |
| 18 | +Here are some examples: |
| 19 | + |
| 20 | +.. code-block:: python |
| 21 | +
|
| 22 | + # obtain NamedCache 'person' |
| 23 | + cache: NamedCache[int, Person] = await session.get_cache("person") |
| 24 | +
|
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + # obtain NamedCache 'person' with a default ttl of 2000 millis |
| 28 | + # any entry inserted into this cache, unless overridden by a put call |
| 29 | + # with a custom ttl, will have a default ttl of 2000 |
| 30 | + options: CacheOptions = CacheOptions(2000) |
| 31 | + cache: NamedCache[int, Person] = await session.get_cache("person", options) |
| 32 | +
|
| 33 | +Near Caches |
| 34 | +=========== |
| 35 | + |
| 36 | +Near caches are a local cache within the `NamedMap` or `NamedCache` |
| 37 | +that will store entries as they are obtained from the remote cache. By doing |
| 38 | +so, it is possible to reduce the number of remote calls made to the Coherence |
| 39 | +cluster by returning the locally cached value. This local cache also |
| 40 | +ensures updates made to, or removal of, an entry are properly |
| 41 | +reflected thus ensuring stale data isn't mistakenly returned. |
| 42 | + |
| 43 | +.. note:: |
| 44 | + Near caching will only work with Coherence CE `24.09` or later. Attempting |
| 45 | + to use near caching features with older versions will have no effect. |
| 46 | + |
| 47 | +A near cache is configured via `NearCacheOptions` which provides several |
| 48 | +options for controlling how entries will be cached locally. |
| 49 | + |
| 50 | + - `ttl` - configures the time-to-live of locally cached entries (this has no |
| 51 | + impact on entries stored within Coherence). If not specified, or the |
| 52 | + `ttl` is `0`, entries in the near cache will not expire |
| 53 | + - `high_units` - configures the max number of entries that may be locally |
| 54 | + cached. Once the number of locally cached entries exceeds the configured |
| 55 | + value, the cache will be pruned down (least recently used entries first) |
| 56 | + to a target size based on the configured `prune_factor` |
| 57 | + (defaults to `0.80` meaning the prune operation would retain 80% of |
| 58 | + the entries) |
| 59 | + - `high_units_memory` - configures the maximum memory size, in bytes, the |
| 60 | + locally cached entries may use. If total memory exceeds the configured |
| 61 | + value, the cache will be pruned down (least recently used entries first) |
| 62 | + to a target size based on the configured `prune_factor` (defaults to |
| 63 | + `0.80` meaning the prune operation would retain 80% the cache memory) |
| 64 | + - `prune_factor` - configures the target near cache size after exceeding |
| 65 | + either `high_units` or `high_units_memory` high-water marks |
| 66 | + |
| 67 | +.. note:: |
| 68 | + `high_units` and `high_units_memory` are mutually exclusive |
| 69 | + |
| 70 | +Examples of configuring near caching: |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + # obtain NamedCache 'person' and configure near caching with a local |
| 75 | + # ttl of 20_000 millis |
| 76 | + near_options: NearCacheOptions = NearCacheOptions(20_000) |
| 77 | + cache_options: CacheOptions = CacheOptions(near_cache_options=near_options) |
| 78 | + cache: NamedCache[int, Person] = await session.get_cache("person", options) |
| 79 | +
|
| 80 | +
|
| 81 | +.. code-block:: python |
| 82 | +
|
| 83 | + # obtain NamedCache 'person' and configure near caching with a max |
| 84 | + # number of entries of 1_000 and when pruned, it will be reduced |
| 85 | + # to 20% |
| 86 | + near_options: NearCacheOptions = NearCacheOptions(high_units=1_000, prune_factor=0.20) |
| 87 | + cache_options: CacheOptions = CacheOptions(near_cache_options=near_options) |
| 88 | + cache: NamedCache[int, Person] = await session.get_cache("person", options) |
| 89 | +
|
| 90 | +To verify the effectiveness of a near cache, several statistics are monitored |
| 91 | +and may be obtained from the `CacheStats` instance returned by the |
| 92 | +`near_cache_stats` property of the `NamedMap` or `NamedCache` |
| 93 | + |
| 94 | +The following statistics are available (the statistic name given is the same |
| 95 | +property name on the `CacheStats` instance) |
| 96 | + |
| 97 | + - hits - the number of times an entry was found in the near cache |
| 98 | + - misses - the number of times an entry was not found in the near cache |
| 99 | + - misses_duration - The accumulated time, in millis, spent for a cache miss |
| 100 | + (i.e., having to make a remote call and update the local cache) |
| 101 | + - hit_rate - the ratio of hits to misses |
| 102 | + - puts - the total number of puts that have been made against the near cache |
| 103 | + - gets - the total number of gets that have been made against the near cache |
| 104 | + - prunes - the number of times the cache was pruned due to exceeding the |
| 105 | + configured `high_units` or `high_units_memory` high-water marks |
| 106 | + - expires - the number of times the near cache's expiry logic expired entries |
| 107 | + - num_pruned - the total number of entries that were removed due to exceeding the |
| 108 | + configured `high_units` or `high_units_memory` high-water marks |
| 109 | + - num_expired - the total number of entries that were removed due to |
| 110 | + expiration |
| 111 | + - prunes_duration - the accumulated time, in millis, spent pruning |
| 112 | + the near cache |
| 113 | + - expires_duration - the accumulated time, in millis, removing |
| 114 | + expired entries from the near cache |
| 115 | + - size - the total number of entries currently held by the near cache |
| 116 | + - bytes - the total bytes the near cache entries consume |
| 117 | + |
| 118 | +.. note:: |
| 119 | + The `near_cache_stats` option will return `None` if near caching isn't |
| 120 | + configured or available |
| 121 | + |
| 122 | +The following example demonstrates the value that near caching can provide: |
| 123 | + |
| 124 | +.. literalinclude:: ../examples/near_caching.py |
| 125 | + :language: python |
| 126 | + :linenos: |
0 commit comments