【redis、java】redis编程

2023-12-15 18:00:04

Java?

package sql;

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

import java.util.Set;

public class redis {
    public static void main(String[] args) {
        JedisPool pool = new JedisPool("localhost", 6379);
        Jedis jedis = pool.getResource();
        jedis.flushDB();
        jedis.set("name", "zhangsan");
        System.out.println(jedis.get("name"));
        Set<String> keys = jedis.keys("*");
        for (String s : keys) {
            System.out.println(s);
        }
        jedis.close();
        pool.close();
    }

}

序列化

目的:为了储存更加复杂的数据结构

package sql;

import org.apache.commons.lang3.SerializationUtils;
import pojo.Video;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class redis {
    public static void main(String[] args) {
        JedisPool pool = new JedisPool("localhost", 6379);
        Jedis jedis = pool.getResource();
        jedis.flushDB();

        Video v1 = new Video();
        byte[] b = SerializationUtils.serialize(v1);
        jedis.set("video:10".getBytes(), b);
        byte[] b1 = jedis.get("video:10".getBytes());
        Video v2 = SerializationUtils.deserialize(b1);
        System.out.println(v2);
        jedis.close();
        pool.close();
    }

}

?

?

文章来源:https://blog.csdn.net/David_Hzy/article/details/134783544
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。