cache.memory is a standardized memory cache bin that can be used to store data in memory, for example as part of a backendchain combined with a persistent cache bin or as an alternative to storing data in a property.
It is backed by the \Drupal\Core\Cache\MemoryCache\MemoryCache backend, which does not serialize data between requests and is therefore faster to use. It does however support in-request cache tag invalidations, which allows to have cached data automatically invalidated when cached in the same request. Cache tag invalidations that happen in separate requests will not affect cache items stored in this bin.
The existing cache bin cache.static is deprecated. It is backed by the \Drupal\Core\Cache\MemoryBackend, which serializes and deserializes cached data to avoid changing it by reference. It is designed to be a drop-in replacement for a persistent cache in Unit or Kernel tests and should not be used for regular runtime operations. The cache.backend.memory cache backend is also deprecated. Update cache configurations, either default_backend in a cache bin service or in $settings.php, to use cache.backend.memory.memory instead.
The Drupal\Core\Cache\MemoryCache\MemoryCacheInterface service is also deprecated, it was incorrectly defined as an alias for the entity.memory_cache which should only be used by entity storages to store loaded entities in memory. If your service relied on Autowire to have this cache bin injected, use an #Autowire attribute instead.
Before:
public function __contruct(
MemoryCacheInterface $cache
) {};
After:
public function __construct(
#[Autowire('cache.memory')]
CacheBackendInterface $cache
) {};
Note: As with other generic cache bins, this is shared across all usages, ensure that unique keys are used by prefixing them with the module, class or similar unique prefixes.