AbstractBackend.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. abstract class Redis_AbstractBackend implements Redis_BackendInterface
  3. {
  4. /**
  5. * Key components name separator
  6. */
  7. const KEY_SEPARATOR = ':';
  8. /**
  9. * @var string
  10. */
  11. private $prefix;
  12. /**
  13. * @var string
  14. */
  15. private $namespace;
  16. /**
  17. * @var mixed
  18. */
  19. private $client;
  20. /**
  21. * Default constructor
  22. *
  23. * @param mixed $client
  24. * Redis client
  25. * @param string $namespace
  26. * Component namespace
  27. * @param string $prefix
  28. * Component prefix
  29. */
  30. public function __construct($client, $namespace = null, $prefix = null)
  31. {
  32. $this->client = $client;
  33. $this->prefix = $prefix;
  34. if (null !== $namespace) {
  35. $this->namespace = $namespace;
  36. }
  37. }
  38. final public function setClient($client)
  39. {
  40. $this->client = $client;
  41. }
  42. final public function getClient()
  43. {
  44. return $this->client;
  45. }
  46. final public function setPrefix($prefix)
  47. {
  48. $this->prefix = $prefix;
  49. }
  50. final public function getPrefix()
  51. {
  52. return $this->prefix;
  53. }
  54. final public function setNamespace($namespace)
  55. {
  56. $this->namespace = $namespace;
  57. }
  58. final public function getNamespace()
  59. {
  60. return $this->namespace;
  61. }
  62. /**
  63. * Get prefixed key
  64. *
  65. * @param string|string[] $parts
  66. * Arbitrary number of strings to compose the key
  67. *
  68. * @return string
  69. */
  70. public function getKey($parts = array())
  71. {
  72. $key = array();
  73. if (null !== $this->prefix) {
  74. $key[] = $this->prefix;
  75. }
  76. if (null !== $this->namespace) {
  77. $key[] = $this->namespace;
  78. }
  79. if ($parts) {
  80. if (is_array($parts)) {
  81. foreach ($parts as $part) {
  82. if ($part) {
  83. $key[] = $part;
  84. }
  85. }
  86. } else {
  87. $key[] = $parts;
  88. }
  89. }
  90. return implode(self::KEY_SEPARATOR, array_filter($key));
  91. }
  92. }