redis 安装配置和jedis操作
2016-02-14 18:38
633 查看
redis
1.安装和测试
下载安装下载tar.gz文件http://redis.io/download,切换目录
cp *.tar.gz /usr/local,解压
sudo tar -zxf *.tar.gz, 进入解压后的目录,编译安装
sudo make install,测试
sudo make test。
配置启动
找到配置文件
vi redis.conf进行修改.
注意指定日志文件路径:
logfile /usr/local/redis/log-redis.log,然后使用
tail -f log-redis.log查看日志。
日志级别
loglevel可以根据需要设置。
守护模式
daemonize yes。
redis.conf有非常详细的说明,直接阅读即可。
启动服务器:
./redis-server redis.conf。
启动客户端进行交互:
./redis-cli。
2.使用jedis操作redis
添加Maven依赖:<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency>
直接连接:
Jedis jedis = new Jedis("127.0.0.1", 6379);
注意:直接连接时,jedis对象不能被多个线程同时操作,因此,要在不同线程使用不同对象。但是如果对象太多,同样会出问题。在多线程情况下,考虑使用连接池获取jedis对象,以减少对象数目。
连接池JedisPool
创建JedisPool:
private static JedisPool pool; static { ResourceBundle bundle = ResourceBundle.getBundle("redis"); if (bundle == null) { throw new IllegalArgumentException("[redis.properties] is not found!"); } JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle"))); config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow"))); config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"))); pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port"))); }
连接池配置参数使用properties文件保存:
redis.properties文件:
#最大能够保持idel状态的对象数 redis.pool.maxIdle=200 #当调用borrow Object方法时,是否进行有效性检查 redis.pool.testOnBorrow=true #当调用return Object方法时,是否进行有效性检查 redis.pool.testOnReturn=true #IP redis.ip=127.0.0.1 #Port redis.port=6379
从连接池获取jedis对象
public static Jedis getJedisFromPool() { return pool.getResource(); }
把对象返回给连接池:
jedis.close();
redis操作命令,见redis官方网站。jedis方法名称和redis的操作命令名是相同的。redis的数据都是按照key存储和查询修改的,value支持如下数据类型:
string 字符串:
APPEND GET DEL SET STRLEN DECR/INCR(整数) GETRANGE...
list 列表:
LPUSH/RPUSH LPOP/RPOP ...
set 无序集合:
SADD SMOVE SPOP SMEMBERS SDIFF...
sorted set有序集合:
ZADD ZINCRBY ZRANGE...
hashmap 哈希表:
HDEL HGET HKEYS HLEN HGET ...
详细操作见官网documentation:http://redis.io/documentation
相关文章推荐
- redis练习手册<十一>set主要操作函数小结
- redis密码设置、访问权限控制等安全设置
- redis练习手册<十>hash主要操作函数小结
- redis 启动,关闭,查看状态
- redis练习手册<九>List主要操作函数小结
- redis使用(五):c异步接口
- Redis源码解析:06整数集合
- 亲密接触Redis-第二天(Redis Sentinel)
- 亲密接触Redis-第二天(Redis Sentinel)
- redis练习手册<八>string主要操作函数小结
- redis练习手册<七>Redis键
- ubuntu 安装redis两种方式 教程
- redis练习手册<六>Redis安全
- redis练习手册<五>Redis 命令
- redis源码分析(2)----字典dict
- 集群 Redis 使用实践
- 通过注册表查询Visual C++ Redistributable Package是否安装
- Redis首页
- Redis文档
- Redis社区