Redis installation and simple use

foreword

  For general enterprise-level development, the database uses relational databases Mysql, Oracle and SqlServer. Without exception, in the development process, we must connect to the database through the database driver, and then we can complete the business of adding, deleting, modifying and querying the database. In this article, we will share and learn about redis, a high-performance key-value database, which is also commonly referred to as a memory cache database.

First, install and start under ubuntu

1. Installation

2. Start

3. Check whether the startup is successful

2. Redis data type

  Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: ordered set).

1、String

  String is the most basic type of redis, a key corresponds to a value, and a key can store a maximum of 512MB.

  The String type is binary safe, which means that the String of redis can contain any data, because any data can be converted into binary and stored, such as jpg images, audio files or serialized objects.

2、Hash

  Redis hash is a set of key-value (key=>value) pairs.

  Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects.

3、List

  Redis lists are simple lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list.

4、Set

  Redis's Set is an unordered collection of string type.

  Sets are implemented through hash tables, so adding, deleting, and searching are all O(1).

5、Zset(Sorted set)

  Redis zset, like set, is also a collection of elements of type string, and does not allow duplicate members.

  The difference is that each element is associated with a fraction of type double. Redis sorts the members of the set from small to large through scores.

  The members of zset are unique, but the score can be repeated.

Third, the use of redis (Java language)

Notice:

  First check if port 6379 is open

sxk@ubuntu:~$ netstat -anpt

By default, redis can only be accessed by the local machine. If you want remote access, you need to modify the redis.conf configuration file. Redis installed using apt. The default installation path of the configuration file redis.conf is /etc/redis/redis.conf. Modify this configuration file

sxk@ubuntu:~$ sudo gedit /etc/redis/redis.conf

After the modification is completed, restart the redis service

Shut down the service: /etc/init.d/redis-server stop

Start the service: /etc/init.d/redis-server start

Restart the service: /etc/init.d/redis-server restart

1. Connect to the redis service

import redis.clients.jedis.Jedis;

/**
 * Created by Xiaokai on 2018/4/29.
 *
 */
public class Redisdemo {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.43.230");
        try {
            System.out.println( "Service is running: "+ jedis.ping());
        }catch (Exception e){
            System.out.println( "No connection to this redis service" );
        }
    }
} 

console prints:
  Service is running: PONG

2. String instance

import redis.clients.jedis.Jedis;

/**
 * Created by Xiaokai on 2018/4/29.
 *
 */
public class Redisdemo {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.43.230");
        try {
            System.out.println( "Service is running: "+ jedis.ping());
            jedis.set("Stonegeek", "http://www.cnblogs.com/sxkgeek");
            System.out.println( "The string stored in redis is: "+ jedis.get("Stonegeek" ));
        }catch (Exception e){
            System.out.println( "No connection to this redis service" );
        }
    }
} 

 console prints: 

  The service is running: PONG
  redis stores the string as: http://www.cnblogs.com/sxkgeek

 

3. Redis Key instance

import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.Set;

/**
 * Created by Xiaokai on 2018/4/29.
 */
public class RedisKey {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.43.230");
        try {
            System.out.println( "Service is running: "+ jedis.ping());
            Set<String> keys = jedis.keys("*");
            Iterator<String> it=keys.iterator() ;
            while(it.hasNext()){
                String key = it.next();
                System.out.println(key);
            }
        }catch (Exception e){
            System.out.println( "No connection to this redis service" );
        }
    }
} 

console prints:

  Service is running: PONG
  stonegeekset
  name
  myhash
  Stonegeek
  stonegeekzset
  stonegeek

 

  In the future, I will continue to share with you the in-depth understanding of learning redis! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325122592&siteId=291194637