CacheInterface.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Contracts\Cache;
  11. use Psr\Cache\CacheItemInterface;
  12. use Psr\Cache\InvalidArgumentException;
  13. /**
  14. * Covers most simple to advanced caching needs.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. interface CacheInterface
  19. {
  20. /**
  21. * Fetches a value from the pool or computes it if not found.
  22. *
  23. * On cache misses, a callback is called that should return the missing value.
  24. * This callback is given a PSR-6 CacheItemInterface instance corresponding to the
  25. * requested key, that could be used e.g. for expiration control. It could also
  26. * be an ItemInterface instance when its additional features are needed.
  27. *
  28. * @param string $key The key of the item to retrieve from the cache
  29. * @param callable|CallbackInterface $callback Should return the computed value for the given key/item
  30. * @param float|null $beta A float that, as it grows, controls the likeliness of triggering
  31. * early expiration. 0 disables it, INF forces immediate expiration.
  32. * The default (or providing null) is implementation dependent but should
  33. * typically be 1.0, which should provide optimal stampede protection.
  34. * See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
  35. * @param array &$metadata The metadata of the cached item {@see ItemInterface::getMetadata()}
  36. *
  37. * @return mixed
  38. *
  39. * @throws InvalidArgumentException When $key is not valid or when $beta is negative
  40. */
  41. public function get(string $key, callable $callback, float $beta = null, array &$metadata = null);
  42. /**
  43. * Removes an item from the pool.
  44. *
  45. * @param string $key The key to delete
  46. *
  47. * @throws InvalidArgumentException When $key is not valid
  48. *
  49. * @return bool True if the item was successfully removed, false if there was any error
  50. */
  51. public function delete(string $key): bool;
  52. }