/~\ C oo _( ^) kiki / ~\
A lightweight, flexible key generator for NoSQL / Key-Value stores, such as Redis, Memcached, Riak, Tokyo Cabinet, etc.
$ gem install kiki
class User include Kiki cache_key :redis cache_key :memcached, :domain => 'people', :delimiter => '/', :identifier => :username end User.redis_key # => #<Keytar::Key:0x000001008ea9a8 @namespace="redis", @delimitter=":", @domain="users", @identifier=:id> User.redis_key("count") # => redis:users:count User.find(1).redis_key # => redis:users:1 User.find(1).redis_key("items") # => redis:users:1:items User.find(1).memcached_key # => memcached/people/schneems # Configuration can be done on the key object User.memcached_key.identifier = :email User.find(1).memcached_key # => email@example.com
the first argument for cache_key, the instance and class method +[namespace]_key+ will be added, which generate keys prefixed by the namespace (eg. “redis” would add the method “redis_key”, generating a key like “redis:users:1”)
optional, defaults to the downcase, pluralized name of your class (eg. “class User” would have the domain “users”)
optional, defaults to “:”
this is the method that is sent when calling “*_key” on an instance, or passing non-string / non-numeric arguments into a “*_key” method. optional, defaults to “:id”
This project was inspired by Richard Schneeman's awesome library Keytar (github.com/schneems/keytar). Essentially, this my alternative take on the problem, focusing on simplicity of API and implementation.
kiki is licensed under the MIT License:
Copyright © 2011 Mattt Thompson (mattt.me)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.