Redis

redis

1.0 概念

  redis是一款高性能的NOSQL系列的非关系型数据库

  关系型和非关系型数据库的区别?

      
 

2.0 下载和安装

  

3.0 redis数据结构的介绍

 

 

 命令操作

 

 

 

 

 

 

 

 

 

 

 

 

 4.0 redis 持久化机制

 

 

      

 

 

Jedis

1.0 快速入门

2.0 Jedis五种类型的操作

1.字符串类型

 

import org.junit.Test;
import redis.clients.jedis.Jedis;

public class test
{
    @Test
    public void Jedis()
    {
        Jedis jedis = new Jedis("localhost",6379);
        jedis.set("username","zhangsan");
        String s = jedis.get("username");//zhangsan
        System.out.println(s);
        jedis.close();

    }
}

 

2.哈希类型  

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.Map;
import java.util.Set;

public class test
{
    @Test
    public void Jedis()
    {
        Jedis jedis = new Jedis("localhost",6379);
        jedis.hset("user","name","lisi");
        jedis.hset("user","age","18");
        jedis.hset("user","gender","male");

        String name = jedis.hget("user", "name");
        System.out.println(name);
        Map<String, String> user = jedis.hgetAll("user");
        Set<String> set = user.keySet();
        for (String key : set) {
            String value = user.get(key);
            System.out.println("key:"+key+" "+"value:"+value);
        }
        jedis.close();
    }
}

  

3.列表类型

 

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;

public class test
{
    @Test
    public void Jedis()
    {
        Jedis jedis = new Jedis("localhost",6379);
        jedis.lpush("mylist","a","b","c");
        jedis.rpush("mylist","a","b","c");
        List<String> mylist = jedis.lrange("mylist", 0, -1);
        String list = mylist.toString();
        System.out.println(list);//[c, b, a, a, b, c]
        jedis.lpop("mylist");
        jedis.rpop("mylist");
        mylist = jedis.lrange("mylist", 0, -1);
         list = mylist.toString();
        System.out.println(list);//[b, a, a, b]
        jedis.close();
    }
}

 

4.集合类型  

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

public class test
{
    @Test
    public void Jedis() {
        Jedis jedis = new Jedis("localhost", 6379);
        jedis.sadd("myset", "a", "b", "c");
        Set<String> smembers = jedis.smembers("myset");
        String s = smembers.toString();
        System.out.println(s);
         jedis.srem("myset", "a");
        smembers=jedis.smembers("myset");
        String s1 = smembers.toString();
        System.out.println(s1);
    }
}

5.有序集合类型

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

public class test
{
    @Test
    public void Jedis() {
        Jedis jedis = new Jedis("localhost", 6379);
        jedis.zadd("mysortlist",90,"lisi");
        jedis.zadd("mysortlist",40,"lihua");
        jedis.zadd("mysortlist",65,"tom");

        Set<String> set = jedis.zrange("mysortlist", 0, -1);
        String toString = set.toString();
        System.out.println(toString);
         jedis.zrem("mysortlist", "lisi");
        set = jedis.zrange("mysortlist", 0, -1);
        toString = set.toString();
        System.out.println(toString);

        jedis.close();
    }
}

  

3.Jedis连接池的使用 需要导入slf4j包

快速入门

 

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class test{
    public static void main(String[] args) {
        Jedis();
    }
    public static void Jedis()
    {
        //Jedis连接池对象
        JedisPool jedisPool = new JedisPool();
        //获取Jedis
        Jedis jedis  = jedisPool.getResource();

        jedis.set("name","v");

        String name = jedis.get("name");
        System.out.println(name);
        //关闭,归还给连接池
         jedis.close();

    }
}

配置文件 jedis.properties

  

 

host=127.0.0.1
post=6379
maxTotal=50
maxIdle=10

  

 Jedis 工具类

package utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.Properties;

public class JedisPoolutils {
    private static JedisPool jedisPool;
    static{
        Properties prop = new Properties();
        try {
            prop.load(JedisPoolutils.class.getClassLoader().getResourceAsStream("jedis.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(prop.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(prop.getProperty("maxIdle")));
        jedisPool = new JedisPool(config,prop.getProperty("host"), Integer.parseInt(prop.getProperty("post")));
    }
    public static Jedis getConnection()
    {
        return jedisPool.getResource();
    }
}

 

定义一个JedisPoolutils的测试类 test

 

import redis.clients.jedis.Jedis;
import utils.JedisPoolutils;

public class test{
    public static void main(String[] args) {
        Jedis();
    }
    public static void Jedis()
    {
//        //Jedis连接池对象
//        JedisPool jedisPool = new JedisPool();
//        //获取Jedis
//        Jedis jedis  = jedisPool.getResource();
        Jedis jedis = JedisPoolutils.getConnection();

        jedis.set("name","JK");

        String name = jedis.get("name");
        System.out.println(name);
        //关闭,归还给连接池
         jedis.close();

    }
}

 

  

 

  

 

posted @ 2020-10-17 10:38  YUNNEN  阅读(113)  评论(0编辑  收藏  举报