【java 操作redis】java连接操作redis

增加maven 依赖

http://maven.outofmemory.cn/redis.clients/jedis/2.6.1/

<dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.6.1</version>
        </dependency>

    </dependencies>

windows 启动本地redis

打开一个 cmd 窗口 使用cd命令切换目录到 C:\redis ,执行命令

redis-server.exe redis.windows.conf

RedisJava.java

import redis.clients.jedis.Jedis;
import java.util.List;
public class RedisJava {
    public static void main(String[] args) {
        //Connecting to Redis server on localhost
        Jedis jedis = new Jedis("localhost");

        System.out.println("Connection to server sucessfully");
        //check whether server is running or not
        System.out.println("Server is running: "+jedis.ping());

        //set the data in redis string
//        jedis.set("tutorialname", "Redis tutorial");

        jedis.del("tutorial-name");
        // Get the stored data and print it
        System.out.println("Stored string in redis:: "+ jedis.get("tutorialname"));

        //store data in redis list
        jedis.lpush("tutorial-list", "Redis");
        jedis.lpush("tutorial-list", "Mongodb");
        jedis.lpush("tutorial-list", "Mysql");
        // Get the stored data and print it
        List<String> list = jedis.lrange("tutorial-list", 0 ,5);

        for(int i = 0; i<list.size(); i++) {
            System.out.println("Stored string in redis:: "+list.get(i));
        }
    }


}

运行结果:

Connection to server sucessfully
Server is running: PONG
Stored string in redis:: Redis tutorial
Stored string in redis:: Mysql
Stored string in redis:: Mongodb
Stored string in redis:: Redis
Stored string in redis:: Mysql
Stored string in redis:: Mongodb
Stored string in redis:: Redis

Process finished with exit code 0
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页