Redis @ SmartOS
Redis也是一套NoSQL資料庫,使用key-value的方式來儲存string, hashes, lists, sets...
下面是官方介紹Redis的一段說明:
Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can containstrings, hashes, lists, sets and sorted sets.
一如往常,SmartOS上安裝Redis只有兩個步驟:
# pkgin search redisphp5-redis-5.2.17.2.1.3 PHP extension for Redisphp53-redis-5.3.6.2.1.3 PHP extension for Redisredis-2.2.14 Persistent key-value database with built-in net interface=: package is installed and up-to-date<: package is installed but newer version is available>: installed package has a greater version than available package# pkgin install redis-2.2.14calculating dependencies... done.nothing to upgrade.1 packages to be installed: redis-2.2.14 (406K to download, 1316K to install)proceed ? [y/N] ydownloading packages...downloading redis-2.2.14.tgz: 203Kbps 100%error log can be found in /var/db/pkgin/err.loginstalling packages...installing redis-2.2.14...redis-2.2.14: Creating group ``redis''redis-2.2.14: Creating user ``redis''redis-2.2.14: copying /opt/local/share/examples/redis/redis.conf.example to /opt/local/etc/redis.conf===========================================================================The following files should be created for redis-2.2.14:/etc/rc.d/redis (m=0755)[/opt/local/share/examples/rc.d/redis]===========================================================================//////////////////////////////////////////////////////////////////////////////This package is SMF enabled, which means you can use SMF to 'enable','disable' or 'restart' the persistent daemon process, e.g.:svcadm enable redisThe SMF manifest was automatically imported now.See our wiki on what's SMF and how to use it to your advantage://////////////////////////////////////////////////////////////////////////////processing local summary...updating database: 100%marking redis-2.2.14 as non auto-removable
因Redis預設在安裝時候設定成SMF服務,安裝完成後可以使用svcadm啓動他:
svcadm enable redis
Redis預設提供了一個CLI介面,安裝完成後,可透過redis-cli啓動
# redis-cliredis 127.0.0.1:6379>redis 127.0.0.1:6379> ?redis-cli 2.2.14Type: "help @<group>" to get a list of commands in <group>"help <command>" for help on <command>"help <tab>" to get a list of possible help topics"quit" to exit
PS: 在官方文件中有提供簡單的教程:http://redis.io/topics/data-types-intro
這邊實作一下於Redis置入string物件的CLI方式
redis 127.0.0.1:6379> set mykey "simon1"OKredis 127.0.0.1:6379> get mykey"simon1"redis 127.0.0.1:6379> set mykey2 "simon2"OKredis 127.0.0.1:6379> get mykey2"simon2"
同時,Redis也提供許多語言的連接實作,可在這邊找到:http://redis.io/clients
而官網建議的clinet實作for Node.js版本為:https://github.com/mranney/node_redis
下面是node_redis client的範例: (redis.print為callback function)
#redis_example.jsvar redis = require("redis"),client = redis.createClient();client.on("error", function (err) {console.log("Error " + err);});client.set("string key", "string val", redis.print); //儲存string型態物件client.get("string key", redis.print); //取回string型態物件client.hset("hash key", "hashtest 1", "some value", redis.print); //儲存Hash型態物件client.hset(["hash key", "hashtest 2", "some other value"], redis.print);client.hkeys("hash key", function (err, replies) { //取回Hash型態物件值console.log(replies.length + " replies:");replies.forEach(function (reply, i) {console.log(" " + i + ": " + reply);});client.quit();});
範例執行結果:
#node redis_example.jsReply: OKReply: string valReply: 1Reply: 12 replies:0: hashtest 11: hashtest 2
承上,redis為與installed server互動的一個NoSQL資料形態,網路上介紹的大部份是屬於執行於redis server上的應用,目前關於redis的server不多的應用不多,相關的實作資訊如下:
- RestMQ,使用Redis實作之MQ Server,並提供REST介面: http://www.restmq.com/
- 不確定是為完成還是未開始的Redis REST: https://github.com/antoniojrossi/redis-rest
以上,Redis是一套不錯的NoSQL系統,相信在許多應用系統上可以使用Redis實作,但因為他相對為較基礎之資料結構實作,不像資料庫系統擁有許多像cluster, load balance, replicate, grid...的功能,但是卻擁有他單純的好處。