Springboot redis hash操作demo

pom依赖:

		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-pool2</artifactId>
	    </dependency>
	    
	     <!--添加junit环境的jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <scope>test</scope>
		</dependency>

配置文件:

server:
  port: 9999
  tomcat:uri-encoding :  UTF-8 
  context-path: /hsj
      
spring:
  application:
    name: hsj
  http:
    encoding:
      force: true
      charset: utf-8
      enabled: true
  redis:
    host: 127.0.0.1
    port: 6739
    password: xfxMaster973339_2018
    database: 0
    lettuce:
      pool:
        max-active: 32
        max-wait: 300ms
        max-idle: 16
        min-idle: 8    

java代码:

package hsj.test.redis.func;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {
	@Autowired
    StringRedisTemplate stringRedisTemplate;
	//hash 名字
	private static final String KEY = "HSJ_REDIS_TEST";
	//每次更新redis的hash,根据key更新value,同时判断距离上一次2分钟,那么同时更新mysql数据库
	private static final long millisecond_of_2minute = 120000L;
	//==判断是不是学习结束或者停止==
	//定时器4分钟执行获取hash一次,目前判断是不是已经停止学习了
	private static final long millisecond_of_4minute = 240000L;
	//获取hash之后循环清理大于5分钟之内没有活动的数据,同时更新mysql数据库
	private static final long millisecond_of_5minute = 300000L;
	
	@org.junit.Test
    public void setRedisMap() {
        {
            try {
            	//=====================hash=============================
            	
            	//----首先按照key获取value旧值----
            	String valueGl = (String) stringRedisTemplate.opsForHash().get(KEY,"4_1_1");  
            	System.out.println("按照key获取value旧值"+valueGl);
            	long nowTimeGl = System.currentTimeMillis();
				long lastTimeGl = Long.parseLong(valueGl.split("")[0]);
				//同时判断距离上一次2分钟,那么同时更新mysql数据库
                if(nowTimeGl - lastTimeGl > millisecond_of_2minute){
					//更新数据库 mysql
					// TODO
                }
            	
                
            	//-----然后按照键key存入新值value---
            	//KEY:userId_courseId_videoId
            	//VALUE:studyLong_updateTime
            	stringRedisTemplate.opsForHash().put(KEY,"4_1_1",System.currentTimeMillis() + "_5.4"); 
            	stringRedisTemplate.opsForHash().put(KEY,"4_1_2",System.currentTimeMillis() + "_7.4"); 
            	stringRedisTemplate.opsForHash().put(KEY,"4_1_2",System.currentTimeMillis() + "_8.8");
            	
            	//---最后定时器,全部获取判断是不是停止学习---
            	//3分钟执行一次,清理
            	Map<Object,Object> map = stringRedisTemplate.opsForHash().entries(KEY);  
            	if(map != null && map.size() > 0){
            		Iterator<Entry<Object, Object>> it = map.entrySet().iterator();
            		while (it.hasNext()) {
						Entry<Object, Object> entry = (Entry<Object, Object>) it.next();
						String key = (String) entry.getKey();
						String value = (String) entry.getValue();
						System.out.println("全部获取  KEY:"+key + "  " + "value:" + value);

						//5分钟没有活动的数据-millisecond_of_15minute
						long nowTime = System.currentTimeMillis();
						long lastTime = Long.parseLong(value.split("")[0]);
                        if(nowTime - lastTime > millisecond_of_5minute){
    						//根据key删除
    						stringRedisTemplate.opsForHash().delete(KEY,key); 
    						//更新数据库 mysql
    						// TODO
                        }
                        
					}
            	}
            	
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(redis,java)