NoSQL databases learning (redis)

A, Nosql database

Commonly used NoSQL have redis, mogoDB and so on, this time to redis learning
all learning content reference rookie tutorial

1.1 Features

  1. Advantages: Fast query, often used as a cache processing, scalable, flexible data model, high availability
  2. Weaknesses: The lack of structured data storage

1.2 Classification

  1. Key (key-value) is stored
  2. Column stores
  3. Document database
  4. Graphics Database

Here Insert Picture Description

1.3 redis data types

  • String type
  • Hash type
  • List Type
  • Indexed collections

1.4 redis scenarios

  1. Cache
  2. Task Queue
  3. Website statistics
  4. Data processing expired
  5. Application Ranking
  6. Distributed cluster architecture separate session

Second, the use redis

Installation Reference: Installation redis

2.1 install redis (windows environment)

  1. Download redis
    you can download the installation according to the actual situation
    Here Insert Picture Description
    (a technique to tell you install these software are generally outside the network, download a long time to download may not down, very uncomfortable, so we can put the download link copy it, then paste it into Thunder in, one second is just great)
    Here Insert Picture Description
  2. Uncompress the file, and save it to disk c
    Here Insert Picture Description
  3. Use the command prompt, enter the directory, use the temporary service installation command redis-server.exe redis.windows.conf, see the following manner, indicating the success of the launch.
    Here Insert Picture Description

It should be note that this window we opened a redis server, so you want to keep this window, we open another window, the same is to enter the directory, enter the command redis-cli

Here Insert Picture Description

Use redis 2.2 Linux environment (to be updated)

To be added. . .

Third, the simple use of redis

3.1 Setting key-value pair

Here Insert Picture Description

3.2 View property configuration

Reference: Redis configuration
view all the configuration information:CONFIG GET *

Here Insert Picture Description

3.3 redis commonly used commands

Numbering command description Examples
1 set key value Setting a key (name of key), the value of value Here Insert Picture Description
2 get key Obtaining a value corresponding key Here Insert Picture Description
3 of the key Delete key Here Insert Picture Description
4 keys * Viewing current database which there are key-value pairs Here Insert Picture Description

Initial use, currently only use these, more details can be found in the documentation:http://doc.redisfans.com/

3.4 jedis use

3.4.1 jedis Download

jedis download
using the Java Connector redis, there are two development kit used.

Share jar package download link: jedis two jar package download
Here Insert Picture Description

3.4.2 using java connected redis

Here I use a unit test to achieve

package com.imooc.jedis;

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * jedis 测试
 * @author Gorit
 * */
public class JedisDemo1 {
	@Test
	/**
	 * 	单实例测试
	 * */
	public void demo1() {
		// 1. 设置 id 和端口号
		Jedis jedis = new Jedis("127.0.0.1",6379);
		// 2. 保存数据
		jedis.set("name", "imooc");
		// 3.  获取数据
		System.out.println(jedis.get("name"));
		jedis.close();
	}
	
	/**
	 * 类似 jdbc 连接池操作
	 * */
	@Test
	public void demo2() {
		// 获取连接池的配置对象
		JedisPoolConfig config = new JedisPoolConfig();
		// 设置最大连接数
		config.setMaxTotal(10);
		// 设置最大空闲连接数
		config.setMaxIdle(10);
		
		// 获得连接池
		JedisPool jdp = new JedisPool(config, "127.0.0.1", 6379);
		// 获取核心对象
		Jedis jedis = null;
		try {
			// 通过连接池获得连接
			jedis = jdp.getResource();
			// 设置数据
			jedis.set("name","张三");
			// 获得数据
			jedis.get("name");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// 释放资源
			if (jedis != null) {
				jedis.close();
			}
			
			if (jdp != null) {
				jdp.close();
			}
		}
	}
}
Published 128 original articles · won praise 233 · Views 100,000 +

Guess you like

Origin blog.csdn.net/caidewei121/article/details/104969255