IpCacheContext.php 644 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. /**
  5. * Defines the IpCacheContext service, for "per IP address" caching.
  6. *
  7. * Cache context ID: 'ip'.
  8. */
  9. class IpCacheContext extends RequestStackCacheContextBase implements CacheContextInterface {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static function getLabel() {
  14. return t('IP address');
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getContext() {
  20. return $this->requestStack->getCurrentRequest()->getClientIp();
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function getCacheableMetadata() {
  26. return new CacheableMetadata();
  27. }
  28. }