Data storage and caching 1-redis-session-cache-C

1. redis
learn the redis standard, and can design and build a redis cache system in a new environment 2. memcached 3. MongoDB 1.

redis redis is an in-memory database, different from a relational database, redis is in the form of key-value key-value pairs database. Operating redis commands is relative to sql. Compared to memcached, Redis runs on a single thread. Redis is written in C language (when can a Chinese person write a world-class open source software). There are 5 types of data stored in redis: strings, hashes, linked lists, sets, and ordered sets . Operation data commands String (string): set, get Hash (hash): hset, hget Linked list (List): lpush, lpop sets (sets) : sadd, sremove sorted sets: zadd, zrem 1.1 download and install redis download https://github.com/ServiceStack/redis-windows/blob/master/downloads/redis64-3.0.501 After .zip is decompressed, configure the redis.windows.conf file

































#Configure redis memory size
#maxmemory <bytes>
maxmemory 1024000000

#Configure password
# requirepass foobared
requirepass 111111


#Start redis server
D:\service\redis64-3.0.501>redis-server.exe redis.windows.conf




1.2 redis use-client
#start redis client
D:\service\redis64-3.0.501>redis-cli.exe -p 6379
127.0.0.1:6379> set name byron
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 111111
OK
127.0.0.1:6379> set name byron
OK
127.0.0.1:6379> get name
"byron"
127.0.0.1:6379>




#install windows service
D:\service\redis64-3.0.501>redis-server --service-install redis.windows.conf --l
oglevel verbose
[3288] 09 Jun 22:48:00.926 # Granting read/write access to 'NT AUTHORITY\Network
Service' on: "D:\service\redis64-3.0.501" "D:\service\redis64-3.0.501\"
[3288] 09 Jun 22:48:00.927 # Redis successfully installed as a service.

D:\service\redis64-3.0.501>

After restarting the computer next time, you can directly execute the client command
redis-cli.exe


Common commands: #View
server information
>info #View

server connection status (return pong to indicate normal connection)
>ping #Clear the

database
>flushdb

#Number of keys values
> dbsize #Basic

data operations
>set name test
>get name   
>del name
>exists name
>type name
>keys * // Query keys that meet a specific format
>rename name name1 //renamed key value

1.3 The redis master-slave design
will Copy the redis installation file into multiple copies (depending on the number of redislave to be designed), modify the configuration file redis.windows.conf
# slave1
port 6380
bind 127.0.0.1
requirepass 111180
slaveof 127.0.0.1 6379
masterauth 111111
slave-read-only yes


# slave2
port 6381
bind 127.0.0.1
requirepass 111181
slaveof 127.0.0.1 6379
masterauth 111111
slave-read-only yes


Start redis after setting, you can access the slave's redis redis distributed cluster through
>redis-cli.exe -p 6380 The ruby ​​environment redis is required Distributed cluster-java, set up two new independent master instances, and implement distributed 1.4 redis in jedis using the-java redisjar package:









<dependency>
	    <groupId>redis.clients</groupId>
	    <artifactId>jedis</artifactId>
	    <version>2.9.0</version>
	</dependency>


Java calls redis services, mainly including self-connection, transaction, pipeline, distributed or a combination of the two. Users can use different methods according to different scenarios
package com.byron.redis.client;

import java.util.Arrays;
import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.Transaction;

public class RedisClient {
	/**
	 * normal call
	 */
	public void test1Normal() {
		Jedis jedis = new Jedis("localhost");
		jedis.auth("111111");
	    long start = System.currentTimeMillis();
	    for (int i = 0; i < 100000; i++) {
	        String result = jedis.set("n" + i, "n" + i);
	    }
	    long end = System.currentTimeMillis();
	    System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds");
	    jedis.disconnect();
	}
	
	/**
	 * Transaction call
	 */
	public void test2Trans() {
	    Jedis jedis = new Jedis("localhost");
	    jedis.auth("111111");
	    long start = System.currentTimeMillis();
	    Transaction tx = jedis.multi();
	    for (int i = 0; i < 100000; i++) {
	        tx.set("t" + i, "t" + i);
	    }
	    List<Object> results = tx.exec();
	    long end = System.currentTimeMillis();
	    System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds");
	    jedis.disconnect();
	}
	
	/**
	 * Pipe call
	 */
	public void test3Pipelined() {
	    Jedis jedis = new Jedis("localhost");
	    jedis.auth("111111");
	    Pipeline pipeline = jedis.pipelined();
	    long start = System.currentTimeMillis();
	    for (int i = 0; i < 100000; i++) {
	        pipeline.set("p" + i, "p" + i);
	    }
	    List<Object> results = pipeline.syncAndReturnAll();
	    long end = System.currentTimeMillis();
	    System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds");
	    jedis.disconnect();
	}
	
	/**
	 * Pipeline transaction call
	 */
	public void test4combPipelineTrans() {
	    Jedis jedis = new Jedis("localhost");
	    jedis.auth("111111");
	    long start = System.currentTimeMillis();
	    Pipeline pipeline = jedis.pipelined();
	    pipeline.multi();
	    for (int i = 0; i < 100000; i++) {
	        pipeline.set("" + i, "" + i);
	    }
	    pipeline.exec();
	    List<Object> results = pipeline.syncAndReturnAll();
	    long end = System.currentTimeMillis();
	    System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds");
	    jedis.disconnect();
	}
	
	/**
	 * Distributed call
	 */
	public void test5shardNormal() {
		JedisShardInfo shard1 = new JedisShardInfo("localhost",6379);
		shard1.setPassword("111111");
		JedisShardInfo shard2 = new JedisShardInfo("localhost",6378);
		shard2.setPassword("111178");
	    List<JedisShardInfo> shards = Arrays.asList(shard1,
	            shard2);

	    ShardedJedis sharding = new ShardedJedis(shards);

	    long start = System.currentTimeMillis();
	    for (int i = 0; i < 100000; i++) {
	        String result = sharding.set("sn" + i, "n" + i);
	    }
	    long end = System.currentTimeMillis();
	    System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds");

	    sharding.disconnect();
	}
	
	/**
	 * Distributed pipeline call
	 */
	public void test6shardpipelined() {
		JedisShardInfo shard1 = new JedisShardInfo("localhost",6379);
		shard1.setPassword("111111");
		JedisShardInfo shard2 = new JedisShardInfo("localhost",6378);
		shard2.setPassword("111178");
	    List<JedisShardInfo> shards = Arrays.asList(shard1,
	            shard2);  

	    ShardedJedis sharding = new ShardedJedis(shards);

	    ShardedJedisPipeline pipeline = sharding.pipelined();
	    long start = System.currentTimeMillis();
	    for (int i = 0; i < 100000; i++) {
	        pipeline.set("sp" + i, "p" + i);
	    }
	    List<Object> results = pipeline.syncAndReturnAll();
	    long end = System.currentTimeMillis();
	    System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds");

	    sharding.disconnect();
	}
	
	public void test7shardSimplePool() {
		JedisShardInfo shard1 = new JedisShardInfo("localhost",6379);
		shard1.setPassword("111111");
		JedisShardInfo shard2 = new JedisShardInfo("localhost",6378);
		shard2.setPassword("111178");
	    List<JedisShardInfo> shards = Arrays.asList(shard1,
	            shard2);   

	    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);

	    ShardedJedis one = pool.getResource();

	    long start = System.currentTimeMillis();
	    for (int i = 0; i < 100000; i++) {
	        String result = one.set("spn" + i, "n" + i);
	    }
	    long end = System.currentTimeMillis();
	    pool.returnResource(one);
	    System.out.println("Simple@Pool SET: " + ((end - start)/1000.0) + " seconds");

	    pool.destroy();
	}
	
	/**
	 * Distributed connection pool asynchronous call
	 */
	public void test8shardPipelinedPool() {
		JedisShardInfo shard1 = new JedisShardInfo("localhost",6379);
		shard1.setPassword("111111");
		JedisShardInfo shard2 = new JedisShardInfo("localhost",6378);
		shard2.setPassword("111178");
	    List<JedisShardInfo> shards = Arrays.asList(shard1,
	            shard2);    

	    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);

	    ShardedJedis one = pool.getResource();

	    ShardedJedisPipeline pipeline = one.pipelined();

	    long start = System.currentTimeMillis();
	    for (int i = 0; i < 100000; i++) {
	        pipeline.set("sppn" + i, "n" + i);
	    }
	    List<Object> results = pipeline.syncAndReturnAll();
	    long end = System.currentTimeMillis();
	    pool.returnResource(one);
	    System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds");
	    pool.destroy();
	}
	
	public static void main(String[] args) {
		RedisClient client = new RedisClient();
		// client.test1Normal(); // Simple SET: 9.562 seconds
		// client.test2Trans(); // Transaction SET: 0.757 seconds
		// client.test3Pipelined(); // Pipelined SET: 0.687 seconds
		// client.test4combPipelineTrans(); // Pipelined transaction: 0.787 seconds
		// client.test5shardNormal(); // Simple@Sharing SET: 10.988 seconds
		// client.test6shardpipelined(); // Pipelined@Sharing SET: 0.964 seconds
		// client.test7shardSimplePool(); // Simple@Pool SET: 11.567 seconds
		// client.test8shardPipelinedPool(); // Pipelined@Pool SET: 0.985 seconds
	}
	
}


It can be seen from the above test that the first type of self-connection and the second type of transaction are obviously much faster. Is that a multi-purpose transaction? In fact, if you just insert a piece of data, the self-connection is obviously faster than the transaction.


1.5 Redis usage -spring


1.6 Redis usage-enterprise-level design




Reference website:
https://redis.io/

Redis installation and usage under win7x64http :
//www.cnblogs.com/koal/p/5484916.html Redis

common commands
http://blog.csdn.net/tangsilai/article/details/7477961

Detailed tutorial on using Redishttp :
//www.cnblogs.com/wangyuyu/p/3786236.html

Guess you like

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