RedisPrefixTrait.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Drupal\redis;
  3. use Drupal\Core\Site\Settings;
  4. trait RedisPrefixTrait {
  5. /**
  6. * @var string
  7. */
  8. protected $prefix;
  9. /**
  10. * Get global default prefix
  11. *
  12. * @param string $suffix
  13. *
  14. * @return string
  15. */
  16. protected function getDefaultPrefix($suffix = NULL) {
  17. $ret = NULL;
  18. if ($test_prefix = drupal_valid_test_ua()) {
  19. $ret = $test_prefix;
  20. }
  21. else {
  22. $prefixes = Settings::get('cache_prefix', '');
  23. if (is_string($prefixes)) {
  24. // Variable can be a string which then considered as a default
  25. // behavior.
  26. $ret = $prefixes;
  27. }
  28. else if (NULL !== $suffix && isset($prefixes[$suffix])) {
  29. if (FALSE !== $prefixes[$suffix]) {
  30. // If entry is set and not false an explicit prefix is set
  31. // for the bin.
  32. $ret = $prefixes[$suffix];
  33. }
  34. else {
  35. // If we have an explicit false it means no prefix whatever
  36. // is the default configuration.
  37. $ret = '';
  38. }
  39. }
  40. else {
  41. // Key is not set, we can safely rely on default behavior.
  42. if (isset($prefixes['default']) && FALSE !== $prefixes['default']) {
  43. $ret = $prefixes['default'];
  44. }
  45. else {
  46. // When default is not set or an explicit false this means
  47. // no prefix.
  48. $ret = '';
  49. }
  50. }
  51. }
  52. if (empty($ret)) {
  53. // If no prefix is given, use the same logic as core for APCu caching.
  54. $ret = Settings::getApcuPrefix('redis', DRUPAL_ROOT);
  55. }
  56. return $ret;
  57. }
  58. /**
  59. * Set prefix
  60. *
  61. * @param string $prefix
  62. */
  63. public function setPrefix($prefix) {
  64. $this->prefix = $prefix;
  65. }
  66. /**
  67. * Get prefix
  68. *
  69. * @return string
  70. */
  71. protected function getPrefix() {
  72. if (!isset($this->prefix)) {
  73. $this->prefix = $this->getDefaultPrefix();
  74. }
  75. return $this->prefix;
  76. }
  77. }