扫码一下
查看教程更方便
开始在 java 中使用 redis 前, 我们需要确保已经安装了 redis 服务及 java redis 驱动,且你的机器上能正常使用 java。
首先,安装 redis 的 java 驱动。
import redis.clients.jedis.jedis;
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());
}
}
编译并运行程序。可以根据需要修改你的路径。我们假设 jedis.jar 在当前路径中。
$javac redisjava.java
$java redisjava
connection to server sucessfully
server is running: pong
import redis.clients.jedis.jedis;
public class redisstringjava {
public static void main(string[] args) {
//connecting to redis server on localhost
jedis jedis = new jedis("localhost");
system.out.println("connection to server sucessfully");
//set the data in redis string
jedis.set("jiyikey", "www.jiyik.com");
// get the stored data and print it
system.out.println("stored string in redis:: " jedis.get("site-name"));
}
}
编译和运行上面的程序
$javac redisstringjava.java
$java redisstringjava
connection to server sucessfully
stored string in redis:: www.jiyik.com
import redis.clients.jedis.jedis;
public class redislistjava {
public static void main(string[] args) {
//connecting to redis server on localhost
jedis jedis = new jedis("localhost");
system.out.println("connection to server sucessfully");
//store data in redis list
jedis.lpush("site-list", "jiyik");
jedis.lpush("site-list", "baidu");
jedis.lpush("site-list", "zhihu");
// get the stored data and print it
list list = jedis.lrange("site-list", 0 ,5);
for(int i = 0; i
编译和运行程序
$javac redislistjava.java
$java redislistjava
connection to server sucessfully
stored string in redis:: jiyik
stored string in redis:: baidu
stored string in redis:: zhihu
redis java keys 实例
import redis.clients.jedis.jedis;
public class rediskeyjava {
public static void main(string[] args) {
//connecting to redis server on localhost
jedis jedis = new jedis("localhost");
system.out.println("connection to server sucessfully");
//store data in redis list
// get the stored data and print it
list list = jedis.keys("*");
for(int i = 0; i
编译和运行程序
$javac rediskeyjava.java
$java rediskeyjava
connection to server sucessfully
list of stored keys:: jiyikey
list of stored keys:: site-list
查看笔记
扫码一下
查看教程更方便