mreschke / keystone
Keystone Key/Value Store
README
Keystone是一个基于Redis之上提供键/值存储封装的mrcore Laravel模块。PHP值存储为原生Redis值(集合、列表、哈希等)。这些原生值在基本序列化上提供更快的速度。值从Redis返回到正确的PHP对象。
组织和约定
Keystone是一个键/值数据存储,因此使用基于字符串的键存储所有信息。这些键组织到命名空间中。命名空间遵循PHP vendor/package
命名空间约定。如果没有在配置中定义命名空间,则在存储键时使用默认命名空间 mreschke/foundation
。使用正确的命名空间分配键由您决定。有关详细信息,请参阅下文中的“正确的键布局”。Keystone值可以是字符串
、列表(数组)
、哈希(关联数组)
或任何对象或项的PHP序列化字符串
。由于文件是字符串,因此Keystone还可以存储任何大小和类型的文件!如果值超过一定的字节数限制,则值将作为索引文件存储在文件系统中。
始终尝试以原生Redis格式存储值,因此数组或关联数组是最好的。仅在必要时使用序列化,因为它仅是PHP格式。
当存储许多类似值,如app/api
配置信息时,尝试使用单个哈希
来存储所有值,而不是使用数百万个单独的键。
实例
要使用PHP访问Keystone,您可以使用以下任何方法。这些方法按照优先级顺序排列。正确的PHP代码表明依赖注入始终是最好的方法。
- 依赖注入 -
public function __construct(Mreschke\Keystone\KeystoneInterface $keystone)
- IoC -
App::make('Mreschke\Keystone')
或App::make('Mreschke\Keystone\KeystoneInterface')
- 外观 -
Mreschke::keystone()->get('mykey')
或$keystone = Mreschke::keystone()
或$keystone = Mreschke::keystone()->getInstance()
在存储值时,我不强制或使用压缩。如果您想使用PHP压缩数据,请使用 gzencode() 和 gzdecode()
而不是其他gz*
PHP函数。gzencode()
是与Unix gzip兼容的函数,因此您可以使用所有gz标准查看压缩文件,如 zcat /myfile
或 gzunzip /myfile
。
PHP API 使用
# Namespace Usage ############################################################################ # If no namespace is defined in the config, the default mreschke/foundation namespace is used. # Nearly every keystone method chain can include a ->ns('custom/namespace') to change the namespace. # You can also specify the namespace in the key itself using :: as the separator $keystone->ns('app/dashboard')->put('mykey', 'test'); $keystone->put('app/dashboard::mykey', 'test'); # Values and types ############################################################################ # Keystone can store simple strings, lists (arrays), hashes (associative arrays) # or PHP objects as serialized strings. The value is automatically stored # using the best method. Values are automatically converted back in to their # proper types on ->get()...no need to manually unserialize or convert to arrays. # You can see a values type by running ->type(). # It is RECOMMENDED to use native redis types where possible, so lists and hashes # over serialized objects. This keeps them simple and fast in redis and allows # other non-php applications to read their values. Serialization is PHP ONLY. # It is best if you can represent your values as simple one level associative arrays (redis hash)! # Stored as redis string (unserialized). Integers/Decimals are just strings in redis $keystone->put($key, 'hi there'); $keystone->put($key, 42); # Stored as redis string (but PHP serialized). PHP objects are automatically serialized $keystone->put($key, $existingPHPObject); $keystone->serialize('mykey', 'this is forced serial, use for complex/nested arrays or items'); # Stored as redis lists (arrays) $keystone->put($key, array('one', 'two', 'three'); # Stored as redis hash (associative array) # Can only be one level of arrays, no complex/nested arrays allowed here $keystone->put($key, array('first' => 'Matthew', 'last' => 'Reschke')); # Get the values type (string, list, hash, file) echo $keystone->type($key); # Storage ############################################################################ # Keystone has two backends; redis and flat file. Keystone automatically # determines the storage backend based on type and size. If the value is a # string > 4096 bytes, it is stored in the filesystem. All other values # are stored in redis. Lists and arrays, no matter their size, are stored in redis # for speed purposes. These files are stored in /store/data/Production/data/Keystone # in folders by namespace. These files are given a unique UUID filename, they # also have a matching redis key. The matching redis key has metadata about the # file like its filename. You can view the metadata with the ->fileInfo() function. # Keystone automatically retrieves from the right source, just use ->get(). $keystone->put($key, 'this goes in redis'); $keystone->put($key, str_repeat('this goes in the filesystem becuase its > 4096 bytes', 1000)); $keystone->put($key, gzencode('some huge, compressed file here, goes in filesystem')); echo $keystone->fileInfo($key); # Smart actions for get, pluck, append, forget ############################################################################ # Keystone can perform "smart" and automatic work on stored values. If you call # ->get($key) it returns the entire value, but if you run ->get($key, 'firstname') # it returns only the 'firstname' hash index value, or if serialized, it automatically # unserializes the object and returns only the associative index or object property desired. # So if you ->put($key, $largeObject), you could "pluck" individual items from that object # even though it's serialized...instead of taking the entire object. Keystone does this # automatically based on the stored type...it will automatically unserialize, # pluck/append/forget, then automatically reserialize. This enables you to work on # objects like they are arrays; pushing, appending, removing...all without retrieving # back the entire object only to put it right back again. # Aliases # ------- # ->get($key, 'index') is the same as ->pluck($key, 'index'); # Add instead of Put # ------------------ # For any value type, you can use ->add() instead of ->put() to put the new key # ONLY if not already exists. # Smart actions on an array (native redis list) # This also works if the value is a serialized array! # Serialized items will be unserialized and reserialized automatically! # --------------------------------------------------------------------- # Put entire array into keystone $keystone->put($key, $myArray); # Append to an array $keystone->push($key, 'one item'); $keystone->push($key, ['multiple', 'items', 'pushed']); # Get entire array echo $keystone->get($key); # Get an array entry by index (0-n) echo $keystone->pluck($key, 20); echo $keystone->get($key, 20); # Get an array range echo $keystone->range($key, 0, -1) #all echo $keystone->range($key, 0, 5) #first 5 echo $keystone->range($key, -5) #last 5 # Delete entire key or just one/multi values by array value (NOT index) $keystone->forget($key); $keystone->forget($key, 'one'); $keystone->forget($key, ['delete', 'multiple']); # Check if entire key exists $keystone->exists($key); # Smart actions on an associative array (native redis hash) # This works if the value is a serialized assoc array too! # This works if the value is a serialized object too! # Serialized items will be unserialized and reserialized automatically! # --------------------------------------------------------------------- # Put entire assoc array into keystone $keystone->put($key, $myAssocArray); # Append to an assoc array or object (same syntax for both) $keystone->push($key, ['new index' => 'new value', 'another index' => 'another value']); # Get entire assoc or object echo $keystone->get($key); # Get one or multiples values from assoc array or object echo $keystone->pluck($key, 'email'); echo $keystone->get($key, 'email'); echo $keystone->pluck($key, ['first', 'last', 'email']); echo $keystone->get($key, ['first', 'last', 'email']); # Delete entire object or just one/multi values from assoc or object $keystone->forget($key); $keystone->forget($key, 'one'); $keystone->forget($key, ['delete', 'multiple']); # Check if entire key exists, or assoc/object indexes and properties $keystone->exists($key); $keystone->exists($key, 'email'); # Finding Keys and Values ############################################################################ # Keystone can return a list of keys based on a given query/filter. It can # also return the values within those keys. If returning values, the return # is an associative array whos indexes are the common denominator of keys. # Aliases # ->values() is an alias to ->where() echo $keystone->keys('user:179:*'); echo $keystone->where('user:179:*'); echo $keystone->where(); # all in default namespace echo $keystone->ns('app/api')->where(); # all in app/api namespace # Other features ############################################################################ # Increment an integer by a number $keystone->increment($key); $keystone->increment($key, 10); $keystone->increment($key, -1);
CLI 使用
Keystone附带一个laravel控制台应用程序,位于 ./artisan keystone
我通常在 {{/usr/local/bin/keystone}}
中创建一个bash脚本,如下所示
#!/bin/bash /var/www/mrcore5/System/artisan keystone "$@"
命令并行PHP API,因此请参阅上面的文档以了解更多关于这些项目的详细信息。
keystone get key keystone get key --index=subkey keystone pluck key --index=subkey keystone get key --unserialize keystone get key --index=subkey --unserialize keystone range key --start=3 --end=19 keystone put key --value='this is a string' keystone put key --value='<? [1,2,3]' keystone put key --value='<? [1,2,3]' --serialize keystone put key --value='<? serialize([1,2,3])' keystone put mreschke/test::test --value='<? Sso::user()->find(179)' keystone put mreschke/test::test --value='<? Sso::user()->find(179)' keystone put key --value='<? ["first" => "Matthew", "last" => "Reschke"]' keystone add key --value='same as put but only add if not exist' keystone push key --value='append new array item' keystone push key --index=description --value='set key in a hash' keystone increment mreschke/test::test --value='1' keystone increment mreschke/test::test --value='1' --increment=-1 keystone increment mreschke/test::test --value='1' --increment=100 keystone exists mreschke/test::test keystone exists mreschke/test::test --index='green' keystone type mreschke/test::test keystone keys 'mreschke/metric::*' keystone values 'mreschke/metric::*' keystone values "mreschke/alerts::*" --index=enabled keystone values "mreschke/alerts::*" --index=enabled --value=1 keystone where "remember where is alias to values" keystone forget mreschke/test::test keystone forget mreschke/test::test --index='green' # You can pipe keys into forget to delete multiple keys! Yikes! keystone keys 'mreschke/metric::*' | keystone forget