1.Redis简介
Redis 是一个key-value存储系统。和Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表)、sets(集合)和zsets(有序集合)几种数据类型。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。
2.Redis性能
官方数据:每秒可存110000条 , 每秒可取 81000 条.
3.安装Redis
Redis的代码遵循ANSI-C编写,系统(如Linux ,等)上安装运行。而且Redis并不依赖任何非标准库,也没有编译参数必需添加。
先去官网下载源码:
wget http://redis.googlecode.com/files/redis-2.4.6.tar.gz
解压:
tar –zxvf redis-2.4.6.tar.gz
编译
需要说明的事,redis的安装非常简单,已经有现成的Makefile文件,直接运行make命令即可。
make
make install
Redis 由四个可执行文件:redis-benchmark 、redis-cli 、redis-server 、redis-stat 这四个文件,加上一个redis.conf 就构成了整个redis的最终可用包。它们的作用如下:
redis-server:Redis服务器的daemon启动程序 redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作 redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能 redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况
现在就可以启动redis了,redis只有一个启动参数,就是他的配置文件路径。
redis-server /etc/redis.conf
注意,默认复制过去的redis.conf 文件的daemonize 参数为no ,所以redis不会在后台运行,这时要测试 ,我们需要重新开一个终端。修改为yes 则为后台运行redis。另外配置文件中规定了pid文件,log文件和数据文件的地址,如果有需要先修改,默认log信息定向到stdout.
下面是redis.conf的主要配置参数的意义:
daemonize:是否以后台daemon方式运行 pidfile:pid文件位置 port:监听的端口号 timeout:请求超时时间 loglevel:log信息级别 logfile:log文件位置 databases:开启数据库的数量 save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。 rdbcompression:是否使用压缩 dbfilename:数据快照文件名(只是文件名,不包括目录) dir:数据快照的保存目录(这个是目录) appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。 appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)
这时你可以打开一个终端进行测试了,配置文件中默认的监听端口是6379
我们可以开启一个Redis客户端进行测试
[root@test01 ~]# redis-cli Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected> exit [root@test01 ~]# redis-server /etc/redis.conf [root@test01 ~]# redis-cli redis 127.0.0.1:6379> quit
4.Redis通常操作
通过Java 调用redis API,实现存储和查询:
import com.agd.utils.PropertiesUtils; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisClientPool { public static RedisClientPool redisClientPool = getInstance(); public static JedisPool jedisPool; public static synchronized RedisClientPool getInstance() { if ( null == redisClientPool) { redisClientPool = new RedisClientPool(); } return redisClientPool; } public RedisClientPool() { if ( null == jedisPool) { init(); } } private static JedisPoolConfig initPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(1000 ); jedisPoolConfig.setMaxIdle(300 ); jedisPoolConfig.setMaxWaitMillis(1000 ); jedisPoolConfig.setTestOnBorrow(true ); jedisPoolConfig.setTestOnReturn(true ); return jedisPoolConfig; } public static void init() { JedisPoolConfig jedisPoolConfig = initPoolConfig(); String host = PropertiesUtils.getValue("redis.host" ); int port = Integer.parseInt(PropertiesUtils.getValue( "redis.port" )); int timeout = Integer.parseInt(PropertiesUtils.getValue( "redis.timeout" )); jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout); }
import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import redis.clients.jedis.Jedis; public class RedisClient { public static void setMapDataToRedis(String flag,Map<String,String> mapData) { Jedis redisClient = null ; try { redisClient = RedisClientPool.jedisPool.getResource(); redisClient.hmset(flag,mapData); } catch (Exception e) { RedisClientPool.jedisPool.returnBrokenResource(redisClient); } finally { RedisClientPool.jedisPool.returnResource(redisClient); } } public static void setDataToRedis(String flag,String field,String value) { Jedis redisClient = null ; try { redisClient = RedisClientPool.jedisPool.getResource(); redisClient.hset(flag, field, value); } catch (Exception e) { RedisClientPool.jedisPool.returnBrokenResource(redisClient); } finally { RedisClientPool.jedisPool.returnResource(redisClient); } } public static Map<String,String> getMapData(String flag) { Map<String,String> dataMap = null ; Jedis redisClient = null ; try { redisClient = RedisClientPool.jedisPool.getResource(); dataMap = redisClient.hgetAll(flag); } catch (Exception e) { RedisClientPool.jedisPool.returnBrokenResource(redisClient); } finally { RedisClientPool.jedisPool.returnResource(redisClient); } return dataMap; } public static long deleteData(String flag) { long result = 0 ; Jedis redisClient = null ; try { redisClient = RedisClientPool.jedisPool.getResource(); result = redisClient.del(flag); } catch (Exception e) { RedisClientPool.jedisPool.returnBrokenResource(redisClient); } finally { RedisClientPool.jedisPool.returnResource(redisClient); } return result; } public static String getData(String flag,String field) { String data = null ; Jedis redisClient = null ; try { redisClient = RedisClientPool.jedisPool.getResource(); data = redisClient.hget(flag, field); } catch (Exception e) { RedisClientPool.jedisPool.returnBrokenResource(redisClient); } finally { RedisClientPool.jedisPool.returnResource(redisClient); } return data; } public static void main(String[] args) throws Exception { RedisClient.testMap(); } public void testList() { Jedis redis = RedisClientPool.jedisPool.getResource(); redis.hset("table" , "field1" , "value1" ); redis.hset("table" , "field2" , "value2" ); redis.hset("table" , "field3" , "value3" ); List<String> list = redis.hmget("table" , "field1" , "field2" , "field3" ); for (String tmp : list) { System.out.println(tmp); } } public static void testMap() { Map<String,String> map = new ConcurrentHashMap<String,String>(); for ( int i = 0 ;i < 10000 ;i++) { map.put("field" +i, "value" +i); } if ( null != getData( "table" , "field1" )) { deleteData("table" ); } Map<String,String> maps = getMapData("table" ); System.out.println(maps.size()); setMapDataToRedis("table" ,map); maps = getMapData("table" ); System.out.println(maps.size()); } }