Download and start Redis in Windows. Since there is no official Version of Windows, the Redis project does not officially support Windows. However, the Microsoft Open Technology Group developed and maintained this Windows port for Win64.

  • Download redis for Windows: github.com/microsoftar…

Then choose your preferred zip or MSI version to download, 3.0.504 is recommended here as 3.2.100 is not a stable version

  • After installation, open the installation directory,

  • Double-click Redis-server. exe to start the Redis server

  • Double-click Redis-cli.exe to open the Redis client (used to execute commands and access the server)

The client connects to Redis

  • Connect the redis
Jedis jedis = new Jedis("localhost".6379); 
Copy the code
  • Operating redis
jedis.set("name"."xdr630"); 
jedis.get("name"); 
Copy the code
  • Close the Redis connection
jedis.close();
Copy the code

case

  • Create a new Maven project, which I wrote in Eclipse

  • Add jedis dependencies to POM.xml
<dependencies>
  	<dependency> 
    <groupId>redis.clients</groupId> 
    <artifactId>jedis</artifactId> 
    <version>2.9. 0</version> 
  </dependency>
  </dependencies>
Copy the code
  • Write a test class, such as JedisTest. Java
package com.xdr630;

import org.junit.Test;

import redis.clients.jedis.Jedis;

public class JedisTest {
	@Test
	public void testJedis(a) {
		/ / 1. Connect to redis
		Jedis jedis = new Jedis("127.0.0.1".6379);
		/ / 2. Redis operations
		jedis.set("name"."xdr630");
		//3. Close the connectionjedis.close(); }}Copy the code

The console doesn’t work because the above code only stores the name value in Redis, fetching the stored value

package com.xdr630;

import org.junit.Test;

import redis.clients.jedis.Jedis;

public class JedisTest {
	@Test
	public void testJedis(a) {
		/ / 1. Connect to redis
		Jedis jedis = new Jedis("127.0.0.1".6379);
		/ / 2. Redis operations
// jedis.set("name", "xdr630");
		String name = jedis.get("name");
		System.out.println(name);
		//3. Close the connectionjedis.close(); }}Copy the code

Test success:



The console outputs the stored value



You can also start the Redis client to retrieve the value you just stored:

Operating the List

Pick up where we left off: Write another test class

@Test
	public void testList(a) {
		/ / 1. Connect to redis
		Jedis jedis = new Jedis("127.0.0.1".6379);
		/ / 2. Redis operations
		jedis.lpush("list1"."a"."b"."c");
		jedis.rpush("list1"."x");
		
		
		List<String> list1 = jedis.lrange("list1".0, -1);
		for(String s : list1) {
			System.out.println(s);
		}
		
		System.out.println(jedis.llen("list1"));
		
		System.out.println();
		//3. Close the connection
		jedis.close();
	}
Copy the code

The Hash operation

@Test
	public void testHash(a) {
		/ / 1. Connect to redis
		Jedis jedis = new Jedis("127.0.0.1".6379);
		/ / 2. Redis operations
		jedis.hset("hash1"."a1"."a1");
		jedis.hset("hash1"."a2"."a2");
		jedis.hset("hash1"."a3"."a3");
		
		Map<String, String> hash1 = jedis.hgetAll("hash1");
		
		System.out.println(hash1);
		
		System.out.println(jedis.hlen("hash1"));
		//3. Close the connection
		jedis.close();
	}	
Copy the code