Redis和Jedis简单使用示例
2015-12-31 11:46
911 查看
Redis简单配置和使用
1. windows启动redis-server要配置一下maxheap 10240000,否则报错“To work around this you may either increase the size of the system paging file, or decrease the size of the Redis heap with the --maxheap flag”
2. 使用配置项requirepass xxxxxx为redis配置强密码,参考漏洞《Redis 未授权访问缺陷可轻易导致系统被黑》
3. redis主从配置也很简单,从redis使用配置项slaveof 192.168.0.118 6379配置主redis,如果主redis设置了密码,从redis需使用配置项masterauth xxxxxx配置访问主redis的密码,从redis和主redis都要设置密码。
4. windows redis客户端使用命令redis-cli -h 192.168.0.118 -p 6379 -a password启动
5. 进一步提高性能的措施:读写分离、redis集群
6. redis官网:http://redis.io/
Jedis的简单代码示例
Jedis官网:https://github.com/xetorthio/jedis
Jedis对象不是线程安全的
简单示例
1. windows启动redis-server要配置一下maxheap 10240000,否则报错“To work around this you may either increase the size of the system paging file, or decrease the size of the Redis heap with the --maxheap flag”
2. 使用配置项requirepass xxxxxx为redis配置强密码,参考漏洞《Redis 未授权访问缺陷可轻易导致系统被黑》
3. redis主从配置也很简单,从redis使用配置项slaveof 192.168.0.118 6379配置主redis,如果主redis设置了密码,从redis需使用配置项masterauth xxxxxx配置访问主redis的密码,从redis和主redis都要设置密码。
4. windows redis客户端使用命令redis-cli -h 192.168.0.118 -p 6379 -a password启动
5. 进一步提高性能的措施:读写分离、redis集群
6. redis官网:http://redis.io/
Jedis的简单代码示例
Jedis官网:https://github.com/xetorthio/jedis
Jedis对象不是线程安全的
简单示例
public static void test(String[] args) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(10); config.setMaxIdle(5); config.setTestOnBorrow(true); JedisPool pool = new JedisPool(config, serverIp, serverPort, timeout, passwrod); if (pool != null) { Jedis jedis = pool.getResource(); try { Set<String> keys = jedis.keys("*"); System.out.println(keys); } catch (JedisConnectionException e) { if (jedis != null) { pool.returnBrokenResource(jedis); jedis = null; } } finally { if (jedis != null) { pool.returnResource(jedis); } } } }4. 《Jedis returnResource使用注意事项》提到,不能不管什么情况都一律使用returnResource,其中的示例调整为如下
public static void test(String[] args) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(10); config.setMaxIdle(5); config.setTestOnBorrow(true); JedisPool pool = new JedisPool(config, serverIp, serverPort, timeout, passwrod); Jedis jedis = null; boolean broken = false; if (pool != null) { try { jedis = pool.getResource(); Set<String> keys = jedis.keys("*"); System.out.println(keys); } catch (JedisException e) { broken = handleJedisException(e); throw e; } finally { closeResource(pool, jedis, broken); } pool.destroy(); } } private static boolean handleJedisException(JedisException jedisException) { if (jedisException instanceof JedisConnectionException) { logger.error("Redis connection lost.", jedisException); } else if (jedisException instanceof JedisDataException) { if ((jedisException.getMessage() != null) && (jedisException .getMessage().indexOf("READONLY") != -1)) { logger.error("Redis connection are read-only slave.", jedisException); } else { // dataException, isBroken=false return false; } } else { logger.error("Jedis exception happen.", jedisException); } return true; } /** * Return jedis connection to the pool, call different return methods * depends on the conectionBroken status. */ protected static void closeResource(JedisPool pool, Jedis jedis, boolean conectionBroken) { try { if (conectionBroken) { pool.returnBrokenResource(jedis); } else { pool.returnResource(jedis); } } catch (Exception e) { logger.error("return back jedis failed, will fore close the jedis.", e); destroyJedis(jedis); } } public static void destroyJedis(Jedis jedis) { if ((jedis != null) && jedis.isConnected()) { try { try { jedis.quit(); } catch (Exception e) { } jedis.disconnect(); } catch (Exception e) { } } }关于此代码,原帖下有人回复“是不是没有配置 testOnBorrow = true? 如果配置了这个参数,每次取jedis时,都会测试 jedis.isConnected和ping一下服务端的。”。
相关文章推荐
- redis安装问题小结
- Redis偶发连接失败案例实战记录
- Redis中实现查找某个值的范围
- Redis和Memcached的区别详解
- 分割超大Redis数据库例子
- Redis总结笔记(一):安装和常用命令
- Redis sort 排序命令详解
- 用Redis实现微博关注关系
- redis中修改配置文件中的端口号 密码方法
- 在Ruby on Rails上使用Redis Store的方法
- Redis和Memcache的区别总结
- 在Node.js应用中使用Redis的方法简介
- Redis服务器的启动过程分析
- web 应用中常用的各种 cache详解
- 利用yum安装Redis的方法详解
- 从MySQL到Redis的简单数据库迁移方法
- 为啥懒 Redis 是更好的 Redis
- 利用Redis实现SQL伸缩的方法
- 在Redis数据库中实现分布式速率限制的方法
- PHP+redis实现添加处理投票的方法