Introduction and use of network cache Redis

1. Introduction to Redis     

Redis is an open source, in-memory data structure storage system that can be used as a database, cache, and messaging middleware . It supports many types of data structures, such as: strings, lists, sets, etc. Redis supports data persistence, which can save data in memory on disk, and can be loaded again for use when restarted. It supports data backup, that is, data backup in master-slave mode.


2. Use of Redis

The use of Redis is divided into two parts: the use under Linux and the use under Windows.

1. The first is the installation and use under Linux.

(1) Download, upload, decompress and install Redis


(2) Start the server and the client


(3) Simple test



2. Not much to say about the installation of Redis under Windows, directly to the test code of the java client

【Pom.xml】

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
	<dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
	</dependency>

[java client test code]

package com.xzw.redis;

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
 *
 * @author xzw
 *
 */
public class RedisClient {
	private Jedis jedis;//Non-sliced ​​client connection
	private JedisPool jedisPool;//Non-sliced ​​connection pool
	private ShardedJedis shardedJedis;//Slice client connection
	private ShardedJedisPool shardedJedisPool;//Slice connection pool
	
	public RedisClient(){
		initialPool();
		initialShardedPool();
		shardedJedis = shardedJedisPool.getResource ();
		jedis = jedisPool.getResource();
	}
	
	/**
	 * Initialize the non-sliced ​​pool
	 */
	private void initialPool(){
		//basic pool settings
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(5);
		config.setMaxTotal(20);
		config.setMaxWaitMillis(1000l);
		config.setTestOnBorrow(false);

		jedisPool = new JedisPool(config, "127.0.0.1", 6379);
	}
	
	/**
	 * Initialize the slice pool
	 */
	private void initialShardedPool(){
		//basic pool settings
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(5);
		config.setMaxTotal(20);
		config.setMaxWaitMillis(1000l);
		config.setTestOnBorrow(false);
		//slave link
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		shards.add(new JedisShardInfo("127.0.0.1", 6379, "master"));
		
		//construct the pool
		shardedJedisPool = new ShardedJedisPool(config, shards);
	}
	
	public void show(){
		jedisPool.returnResource(jedis);
		shardedJedisPool.returnResource (shardedJedis);
	}
	
	public static void main(String[] args) {
		new RedisClient().show();
	}
	
}

Guess you like

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