Redis初探
Redis is an advanced key-value store. It is similar to memcached but the dataset is not volatile, and values can be strings, exactly like in memcached, but also lists, sets, and ordered sets. All this data types can be manipulated with atomic operations to push/pop elements, add/remove elements, perform server side union, intersection, difference between sets, and so forth. Redis supports different kind of sorting abilities.
A persistent key-value database with built-in net interface written in ANSI-C for Posix systems
1. 启动server
eg: ./redis-server redis.conf
帮助可以为(因为没有--help):
2. 使用client
eg: ./redis-cli -h 127.0.0.1 -p 6379 -n 0
帮助可以为(因为没有--help):./redis-cli -h
3. redis.conf (redis-server 需要的配置文件)
daemonize no // 是否w为daemon进程,如果是将把pid写pidfile(为shutdown保存pid),
port 6379 // 端口不设置, 默认为6379
bind 127.0.0.1 // ip不设置, 默认为0.0.0.0
timeout 300
loglevel debug
logfile stdout
databases 16 // 分成16个库,dbid为0到16-1
################################ SNAPSHOTTING #################################
save <seconds> <changes> // 多个save的维度: seconds,changes是与的关系, 多个save是或的关系
rdbcompression yes
dbfilename dump.rdb // rdb文件名
dir ./ // rdb文件工作路径
################################# REPLICATION #################################
slaveof <masterip> <masterport> // 复制来源端ip port
masterauth <master-password>
################################## SECURITY ###################################
requirepass foobared
################################### LIMITS ####################################
maxclients 128
maxmemory <bytes> // 最大内存,超出后将不能增加数据
// ...
4. java client libraries projects example:
/**
*
*/
package redis;
import org.jredis.ClientRuntimeException;
import org.jredis.JRedis;
import org.jredis.ProviderException;
import org.jredis.RedisException;
import org.jredis.ri.alphazero.JRedisClient;
/**
* Redis First example
*
* @author yangwm Nov 17, 2010 3:21:36 PM
*/
public class RedisFirst {
public static final String host = "127.0.0.1";
public static final int port = 6379;
public static void main(String[] args) {
try {
// key in your DB 0.
JRedis jredis = new JRedisClient(host, port, "password", 0);
jredis.ping();
jredis.set("yangwmTest", "yangwTestValue123456");
System.out.println(new String(jredis.get("yangwmTest")));
jredis.quit();
} catch (RedisException error) {
error.printStackTrace();
} catch (ProviderException bug) {
System.out.format("Oh no, an 'un-documented feature': %s/nKindly report it.", bug.getMessage());
} catch (ClientRuntimeException problem) {
System.out.format("%s/n", problem.getMessage());
}
}
}
学习参考资料:
http://code.google.com/p/redis/
https://github.com/xetorthio/jedis
http://code.google.com/p/jredis/