Getting Started with Redis (1) - Environment Installation Test and Basic Command Demonstration

redis overview

redis is an open source, advanced key-value store that can be used to build high-performance storage solutions. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hypertext and geospatial indexes with radius queries. NoSQL, Not Only [SQL], generally refers to non-relational databases. So redis is a kind of nosql. Knock on the blackboard to draw the key point: redis is a kind of nosql.

Advantages of redis:

Exceptionally fast
Supports rich data types
Operations are atomic

Download and install

$ wget http://download.redis.io/releases/redis-3.2.6.tar.gz 
/root/redis tar xzf redis−3.2.6.tar.gz
/root/redis/redis-3.2.6   make
启动服务器:

 ```
    $ src/redis-server
 ```

 启动客户端

 ```
 $ src/redis-cli
 ```

start up:

redis-server
redis-cli

11645:C 23 Apr 14:48:49.784 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 11645
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

11645:M 23 Apr 14:48:49.788 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
11645:M 23 Apr 14:48:49.788 # Server started, Redis version 3.2.6
11645:M 23 Apr 14:48:49.788 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
11645:M 23 Apr 14:48:49.788 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
11645:M 23 Apr 14:48:49.788 * DB loaded from disk: 0.000 seconds
11645:M 23 Apr 14:48:49.788 * The server is now ready to accept connections on port 6379

Data types supported by redis

Start the client and store the string to redis.

127.0.0.1:6379> set name wonder
OK

Take a string:

127.0.0.1:6379> get name
"wonder"

hash value

127.0.0.1:6379> hmset king username wonder password root age 22
OK
127.0.0.1:6379> hgetall king
1) "username"
2) "wonder"
3) "password"
4) "root"
5) "age"
6) "22"

Lists - Lists

127.0.0.1:6379> lpush pricess yiyue
(integer) 1
127.0.0.1:6379> lpush pricess chen
(integer) 2
127.0.0.1:6379> lpush privess yinli
(integer) 1
127.0.0.1:6379> lpush pricess yinli
(integer) 3
127.0.0.1:6379> lrange pricess 0 10
1) "yinli"
2) "chen"
3) "yiyue"
127.0.0.1:6379> 

Redis sorted collections

Redis ordered collections are similar to Redis collections stored in the set value uniqueness. The difference is that each member of an ordered set carries a score, which is used to take the ordered set command, from the smallest to the largest score.

127.0.0.1:6379> zadd kindom 1 redis
(integer) 1
127.0.0.1:6379> zadd kindom 2 mongodb
(integer) 1
127.0.0.1:6379> zadd kindom 3 mysql
(integer) 1
127.0.0.1:6379> zadd kindom 3 mysql
(integer) 0
127.0.0.1:6379> zadd kindom 4 mysql
(integer) 0
127.0.0.1:6379> zrange kindom 0 10 withscores
1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"

redis publish subscribe

Turn on the client as a receiver

127.0.0.1:6379> subscribe myking messages
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "myking"
3) (integer) 1
1) "subscribe"
2) "messages"
3) (integer) 2

Start another client as sender:

127.0.0.1:6379> publish myking redis
(integer) 1
127.0.0.1:6379> publish myking "hello redis,you are a great caching technique"
(integer) 1

Note the changes on the subscriber side:

Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "myking"
3) (integer) 1
1) "subscribe"
2) "messages"
3) (integer) 2
1) "message"
2) "myking"
3) "redis"
1) "message"
2) "myking"
3) "hello redis,you are a great caching technique"

some other operations

1. Get all key
KEYS *

127.0.0.1:6379> keys *
1) "kindom"
2) "name"
3) "privess"
4) "king"
5) "pricess"

2. Determine whether the key exists

Returns 1 if EXISTS key
exists, returns 0 if it does not exist

127.0.0.1:6379> exists privess
(integer) 1
127.0.0.1:6379> exists privesssssss
(integer) 0

3. Delete key

DEL key [key]

127.0.0.1:6379> del privess
(integer) 1
127.0.0.1:6379> keys *
1) "kindom"
2) "name"
3) "king"
4) "pricess"

4. Get the data type

TYPE key

127.0.0.1:6379> type kindom
zset

5. Add to the tail

APPEND key value

127.0.0.1:6379> append name qqq
(integer) 9
127.0.0.1:6379> get name
"wonderqqq"

6. Get the length of the string

strlen key

127.0.0.1:6379> strlen name
(integer) 9

Of course, here are just some simple operations, and complex reference official documents.

Using redis-jedis in java applications

The premise is that redis has been installed and the service has been started.

jedis download address https://github.com/xetorthio/jedis

Jedis is a blazingly small and sane Redis java client.
Jedis was conceived to be EASY to use.
Translation: jedis is a very small java client that is considered to be easy to use.

how to use?

import redis.clients.jedis.Jedis;

import java.util.List;

public class TestRedisClient {
    public static void main(String[] args){

        Jedis jedis = new Jedis("10.20.112.33");
        System.out.println("Connection to server sucessfully");
        //check whether server is running or not
        System.out.println("Server is running: "+jedis.ping());
        jedis.lpush("wonder-list", "Redis");
        jedis.lpush("wonder-list", "Mongodb");
        jedis.lpush("wonder-list", "Mysql");
        // Get the stored data and print it
        List<String> list = jedis.lrange("wonder-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:: Mysql
Stored string in redis:: Mongodb
Stored string in redis:: Redis

The above are all installed and tested by themselves.

The problems encountered are:

1. How to use the redis service on linux through remote, Windows.

1. Comment out the bind ip configuration of your redis.conf.
I will comment out the original 127.0.0.1 here.

2. Set protect-mode no

3. Shut down the redis service: redis-server stop or use ctrl + c

4. Start: redis-server your redis.conf directory

When starting, you can specify a configuration file to start

Successfully connected.

Guess you like

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