Learning from jedis


Learning from jedis

The official account of WeChat:Love to ask CTO
Professional programming Q & a community

Introduction to jedis

Redis not only uses commands to operate, but also uses program client to operate. Now basically, the mainstream languages have client support, such as Java, C, C #, C + +, PHP, go, etc
Some Java clients are listed in the official website, and the one most used in enterprises is jedis.

Basic use

public class JedisTest {
    public void testJedis() {
        //Create a connection to jedis
        Jedis jedis = new Jedis("", 6379);
        //Execute the redis command
        jedis.set("k1", "jedis");
        //Value from redis
        String result = jedis.get("k1");
        //Print results
        //Close the connection


View the results after running:


After the jedis object is constructed, the bottom layer of jedis will open a socket channel to connect with the redis service
The frequent creation and destruction of jedis objects has a great impact on performance.

Use of connection pool

    public void testJedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        //Controls the maximum number of jedis instances with idle status in a pool
        //Maximum connections
        //Create a connection pool object
        JedisPool jedisPool = new JedisPool(config,"", 6379);
        //Get connections from connection pool
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get("k1") ;
        //Close the connection
        //Close the connection池

View the results after running:


Jedis connect cluster

    public void testJedisCluster() {
        //Creating a jediscluster object
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("", 7001));
        nodes.add(new HostAndPort("", 7002));
        nodes.add(new HostAndPort("", 7003));
        nodes.add(new HostAndPort("", 7004));
        nodes.add(new HostAndPort("", 7005));
        nodes.add(new HostAndPort("", 7006));
        JedisCluster cluster = new JedisCluster(nodes);
        //The API method of cluster object is used to add and query redis cluster
        cluster.set("c1", "jedisCluster");
        //Release resources

Recommended Today

How to Build a Cybersecurity Career

Original text:How to Build a Cybersecurity Career How to build the cause of network security Normative guidelines for building a successful career in the field of information security fromDaniel miesslerstayinformation safetyCreated / updated: December 17, 2019 I’ve been doing itinformation safety(now many people call it network security) it’s been about 20 years, and I’ve spent […]