فهرست منبع

Add the 'prefix' client option. The client will automatically prepend the specified prefix string to each key.

Daniele Alessandri 14 سال پیش
والد
کامیت
e97e13bde5
4فایلهای تغییر یافته به همراه17 افزوده شده و 4 حذف شده
  1. 1 4
      examples/KeyPrefixes.php
  2. 3 0
      lib/Predis/Client.php
  3. 2 0
      lib/Predis/ClientOptions.php
  4. 11 0
      lib/Predis/Options/ClientPrefix.php

+ 1 - 4
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'));

+ 3 - 0
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);
     }

+ 2 - 0
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;
     }

+ 11 - 0
lib/Predis/Options/ClientPrefix.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace Predis\Options;
+
+use Predis\Commands\Processors\KeyPrefixProcessor;
+
+class ClientPrefix extends Option {
+    public function validate($value) {
+        return new KeyPrefixProcessor($value);
+    }
+}