From e97e13bde59cbee08e9d667cbc924e5d2e5aa655 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Thu, 5 May 2011 17:16:14 +0200 Subject: [PATCH] Add the 'prefix' client option. The client will automatically prepend the specified prefix string to each key. --- examples/KeyPrefixes.php | 5 +---- lib/Predis/Client.php | 3 +++ lib/Predis/ClientOptions.php | 2 ++ lib/Predis/Options/ClientPrefix.php | 11 +++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 lib/Predis/Options/ClientPrefix.php diff --git a/examples/KeyPrefixes.php b/examples/KeyPrefixes.php index 0f5a15a7..4802465a 100644 --- a/examples/KeyPrefixes.php +++ b/examples/KeyPrefixes.php @@ -8,10 +8,7 @@ require 'SharedConfigurations.php'; // user-level namespaces for you keyspace, thus eliminating the need for separate // logical databases. -use Predis\Commands\Processors\KeyPrefixProcessor; - -$client = new Predis\Client(); -$client->getProfile()->setProcessor(new KeyPrefixProcessor('nrk:')); +$client = new Predis\Client($single_server, array('prefix' => 'nrk:')); $client->mset(array('foo' => 'bar', 'lol' => 'wut')); var_dump($client->mget('foo', 'lol')); diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index 15142f93..442ff82a 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -16,6 +16,9 @@ class Client { $options = $this->filterOptions($options); $this->_options = $options; $this->_profile = $options->profile; + if ($prefixer = $options->prefix) { + $this->_profile->setProcessor($prefixer); + } $this->_connectionFactory = $options->connections; $this->_connection = $this->initializeConnection($parameters); } diff --git a/lib/Predis/ClientOptions.php b/lib/Predis/ClientOptions.php index 018cf89c..a4245dca 100644 --- a/lib/Predis/ClientOptions.php +++ b/lib/Predis/ClientOptions.php @@ -3,6 +3,7 @@ namespace Predis; use Predis\Options\IOption; +use Predis\Options\ClientPrefix; use Predis\Options\ClientProfile; use Predis\Options\ClientCluster; use Predis\Options\ClientConnectionFactory; @@ -23,6 +24,7 @@ class ClientOptions { 'profile' => new ClientProfile(), 'connections' => new ClientConnectionFactory(), 'cluster' => new ClientCluster(), + 'prefix' => new ClientPrefix(), ); return self::$_sharedOptions; } diff --git a/lib/Predis/Options/ClientPrefix.php b/lib/Predis/Options/ClientPrefix.php new file mode 100644 index 00000000..962cf7f5 --- /dev/null +++ b/lib/Predis/Options/ClientPrefix.php @@ -0,0 +1,11 @@ +