标签:iterator com fonts fill 可变参数 put 访问 判断 --
上一篇总结我们使用我们本地的Eclipse中创建的jedis工程,链接
到了我们处于VMware虚拟机上的Linux系统上的Redis服务,我们接下来
讲一下jedis的一些常用的API。
(1)jedis存储字符串
- package cn.com.redis;
-
- import redis.clients.jedis.Jedis;
-
- public class Test1 {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("192.168.248.129",6379);
-
-
- jedis.set("username", "jack");
-
- System.out.println(jedis.get("username"));
-
- jedis.append("username"," is a Coder");
- System.out.println(jedis.get("username"));
-
- jedis.del("username");
- System.out.println(jedis.get("username"));
-
- jedis.mset("username","liuling","age","23","qq","412345678");
- jedis.incr("age");
- System.out.println(jedis.get("name")+"-"+jedis.get("age")+"-"+jedis.get("qq"));
- }
- }
效果:
查看一下redis:
说明我们的操作是成功的。
(2)jedis操作Map
- package cn.com.redis;
-
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
-
- import redis.clients.jedis.Jedis;
-
- public class Test2 {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("192.168.248.129",6379);
-
-
- Map<String,String> map = new HashMap<String,String>();
- map.put("name", "ZhangSan");
- map.put("age", "35");
- map.put("email", "12345678@126.com");
- jedis.hmset("user",map);
-
-
-
- List<String> rsmap = jedis.hmget("user", "name","age","email");
- System.out.println(rsmap);
-
-
- jedis.hdel("user","age");
- System.out.println(jedis.hmget("user", "age"));
- System.out.println(jedis.hlen("user"));
- System.out.println(jedis.exists("user"));
- System.out.println(jedis.hkeys("user"));
- System.out.println(jedis.hvals("user"));
-
- Iterator<String> iter=jedis.hkeys("user").iterator();
- while(iter.hasNext()){
- String key = iter.next();
- System.out.println(key+":"+jedis.hmget("user", key));
- }
- }
- }
效果:
查看一下redis:
说明我们的操作是成功的。
(3)jedis操作List
- package cn.com.redis;
-
- import redis.clients.jedis.Jedis;
-
- public class Test3 {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("192.168.248.129",6379);
-
-
- jedis.del("java framework");
- System.out.println(jedis.lrange("java framework", 0, -1));
-
-
-
- jedis.lpush("java framework","spring");
- jedis.lpush("java framework","struts");
- jedis.lpush("java framework","hibernate");
-
-
-
- System.out.println(jedis.lrange("java framework", 0, -1));
-
-
- System.out.println(jedis.llen("java framework"));
-
-
- jedis.del("java framework");
-
-
-
- jedis.rpush("java framework", "spring");
- jedis.rpush("java framework", "struts");
- jedis.rpush("java framework", "hibernate");
- System.out.println(jedis.lrange("java framework", 0, -1));
-
- }
- }
效果:
查看以下redis:
说明我们的操作是成功的。
(4)jedis操作Set
- package cn.com.redis;
-
- import redis.clients.jedis.Jedis;
-
- public class Test4 {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("192.168.248.129",6379);
-
-
- jedis.sadd("users", "ZhangSan");
- jedis.sadd("users", "LiSi");
- jedis.sadd("users", "WangWu");
- jedis.sadd("users", "ZhangEr");
- jedis.sadd("users", "MaLiu");
-
-
- jedis.srem("users", "MaLiu");
- System.out.println(jedis.smembers("users"));
-
- System.out.println(jedis.sismember("users", "MaLiu"));
- System.out.println(jedis.srandmember("users"));
- System.out.println(jedis.scard("users"));
-
- }
- }
效果:
查看以下redis:
说明我们的操作是成功的。
(5)jedis排序
- package cn.com.redis;
-
- import redis.clients.jedis.Jedis;
-
- public class Test5 {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("192.168.248.129",6379);
-
-
-
-
-
- jedis.del("a");
- jedis.rpush("a", "1");
- jedis.rpush("a", "6");
- jedis.rpush("a", "3");
- jedis.rpush("a", "9");
- System.out.println(jedis.lrange("a", 0, -1));
- System.out.println(jedis.sort("a"));
-
- jedis.del("a");
- jedis.lpush("a", "1");
- jedis.lpush("a", "6");
- jedis.lpush("a", "3");
- jedis.lpush("a", "9");
- System.out.println(jedis.lrange("a", 0, -1));
- System.out.println(jedis.sort("a"));
-
- }
- }
效果:
查看以下redis:
说明我们的操作是成功的。
(6)Redis连接池
我们每一次使用redis的时候,都要写Jedis jedis = new Jedis("192.168.248.129",6379);
我们要优化连接,不能使用一次就new一个连接对象,我们要创建一个工具类,管理redis的
连接和一些配置参数的设置。
- package cn.com.redis;
-
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
-
- public class RedisUtil {
-
- private static String ADDR = "192.168.248.129";
-
-
- private static int PORT = 6379;
-
-
- private static String AUTH = "root";
-
-
-
-
- private static int MAX_ACTIVE = 1024;
-
-
- private static int MAX_IDLE = 200;
-
-
-
- private static int MAX_WAIT = 10000;
-
- private static int TIMEOUT = 10000;
-
-
-
- private static boolean TEST_ON_BORROW = true;
-
- private static JedisPool jedisPool = null;
-
-
- static{
- try {
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxTotal(MAX_ACTIVE);
- config.setMaxIdle(MAX_IDLE);
- config.setMaxWaitMillis(MAX_WAIT);
- config.setTestOnBorrow(TEST_ON_BORROW);
- jedisPool = new JedisPool(config,ADDR,PORT,TIMEOUT);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- public synchronized static Jedis getJedis(){
- try {
- if(jedisPool != null){
- Jedis resource = jedisPool.getResource();
- return resource;
- }else{
- return null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
-
- public static void returnResource(final Jedis jedis){
- if(jedis != null){
- jedisPool.returnResourceObject(jedis);
- }
- }
-
- }
不要忘记引入commons-pool.jar:
测试:
- package cn.com.redis;
-
- import redis.clients.jedis.Jedis;
-
- public class Test6 {
- public static void main(String[] args) {
- RedisUtil redisUtil = new RedisUtil();
- Jedis jedis = redisUtil.getJedis();
-
- try {
- System.out.println(jedis.lrange("a", 0, -1));
- System.out.println(jedis.smembers("users"));
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- redisUtil.returnResource(jedis);
- }
-
- }
- }
结果:
使用连接池获取jedis对象会更符合开发规范。
智能转账:http://blog.csdn.net/acmman/article/details/53487684
redis12--常用API
标签:iterator com fonts fill 可变参数 put 访问 判断 --
原文地址:http://www.cnblogs.com/gaochsh/p/6288360.html