当前位置: 首页 > >

ʹÓÃRedisTemplateÅúÁ¿´æÈëÊý¾Ý£¬100Íò²âÊÔ ÐèÒª1·ÖÖÓ

发布时间:

Ŀ¼


1.ÐèÇó


2.»ù±¾ÅäÖÃ


3.ºËÐÄ´úÂë


4.dao


5.mapper/*.xml


6.Êý¾Ý±í


7.¹¤¾ßÀà



²Î¿¼Îĵµ


https://blog.csdn.net/xiaoliu598906167/article/details/82218525


https://blog.csdn.net/fouling/article/details/98631144


https://blog.csdn.net/supersub000/article/details/80100016


1.ÐèÇó

½«mysqlÊý¾Ý¿âÖеÄÉ*ÙÍòÊý¾Ý´æÈëredisÖдæÈëµÄ¸ñʽΪhashMapµÄÐÎʽ
¼´£¬£¨hash·¾¶£¬key, obj£©ÆäÖеÄobjΪmap ·â×°Ò»Ìõ¼Ç¼ÐÅÏ¢
²¢ÇÒÕâЩÊý¾ÝÔÚÓõÄʱºò¿ÉÒÔÈ¡µÃµ½¡£
Äѵ㣬ÆÕͨͨ¹ýÑ­»·hsetµÄ·½Ê½±È½ÏÂý£¬Ò»ÍòÌõÊý¾ÝÐèÒª6Ãë×óÓÒ£¬Ò»°ÙÍòÐèÒª10·ÖÖÓ×óÓÒ
ÓÅ»¯·½°¸£ºredisTemplateµÄ¹ÜµÀapi executePipelined()
×¢Ò⣬ÈÔÐèÒª½«Êý¾Ý½øÐзֶÎÏòredisÖдæÈ루²»È»±»¿¨ËÀ£©£¬±ÈÈç¿ÉÒԷֶδÓÊý¾Ý¿âÈ¡È»ºó·Ö¶Î´æ£¬
?µ«ÊÇ×îºÃÊÇÒ»ÏÂ×ÓÈ¡³öÀ´£¬ÔٷֶδæÈ룬±¾°¸ÀýÊÇ·Ö¶ÎÈ¡·Ö¶Î´æ¡£


2.»ù±¾ÅäÖÃ



? ?org.springframework.boot
? ?spring-boot-starter-data-redis


»ù±¾ÅäÖÃÀࣨ±ØÐ룩


package com.example.studyspringboot.studyboot.utils.stuRedis;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {


@SuppressWarnings("rawtypes")
@Autowired
private RedisTemplate redisTemplate;

/**
* ½â¾öredis²åÈëÖÐÎÄÂÒÂë
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Bean
public RedisTemplate redisTemplateInit() {
//ÉèÖÃÐòÁл¯KeyµÄʵÀý»¯¶ÔÏó
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//ÉèÖÃÐòÁл¯ValueµÄʵÀý»¯¶ÔÏó
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer());
return redisTemplate;
}
}

ÅäÖÃÎļþ



#MYSQLÁ´½Ó
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/redistest?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=lps123
# AOP
# spring.aop.auto=true
# ĬÈÏʹÓÃcglib¶¯Ì¬´úÀí
# Ç¿ÖÆʹÓÃjdk´úÀí£¨²»ÒªÕâÑù×ö£©
#spring.aop.proxy-target-class=false

#mybatisÅäÖÃ
mybatis.mapper-locations = classpath:/mapper/*.xml
#mybatis.type-aliases-package=com.customerNoPlatform.entity

#redisÅäÖÃ
#Redis·þÎñÆ÷µØÖ·
spring.redis.host=127.0.0.1
#Redis·þÎñÆ÷Á¬½Ó¶Ë¿Ú
spring.redis.port=6379
#RedisÊý¾Ý¿âË÷Òý£¨Ä¬ÈÏΪ0£©
spring.redis.database=0
#Á¬½Ó³Ø×î´óÁ¬½ÓÊý£¨Ê¹ÓøºÖµ±íʾûÓÐÏÞÖÆ£©
spring.redis.jedis.pool.max-active=50
#Á¬½Ó³Ø×î´ó×èÈûµÈ´ýʱ¼ä£¨Ê¹ÓøºÖµ±íʾûÓÐÏÞÖÆ£©
spring.redis.jedis.pool.max-wait=3000
#Á¬½Ó³ØÖеÄ×î´ó¿ÕÏÐÁ¬½Ó
spring.redis.jedis.pool.max-idle=20
#Á¬½Ó³ØÖеÄ×îС¿ÕÏÐÁ¬½Ó
spring.redis.jedis.pool.min-idle=2
#Á¬½Ó³¬Ê±Ê±¼ä£¨ºÁÃ룩
spring.redis.timeout=5000

3.ºËÐÄ´úÂë

package com.example.studyspringboot.studyboot.utils.stuRedis;

import com.alibaba.fastjson.JSON;
import com.example.studyspringboot.studyboot.dao.UserDao;
import org.apache.catalina.Pipeline;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class Main {

@Autowired
private RedisTemplate redisTemplate;
@Autowired
private UserDao userDao;
@Autowired RedisUtil redisUtil;

/**
* ¶ÁÈ¡»º´æÊý¾Ý
*/
@Test
public void get() {
// redisUtils.get("908687_name_2726||908687_sex_2726||908687_hobby_2726908687_height_2726");
Map value = (Map)redisUtil.hget("nbs:hello:leaf","908687_name_2726||908687_sex_2726||908687_hobby_2726908687_height_2726");
System.out.println(JSON.toJSONString(value));
System.out.println(value.get("name"));
}

@Test
public void method2() {
CustomersToRedis("nbs:hello:leaf",0);
}
/**
* @Author lps
* @Description //TODO
* @Date 2020-07-25 15:21:44 ÐÇÆÚÁù
* @Param [path, stepSize] pathhashÖµµÄ·¾¶£¬stepSize ²½³¤¶àÉÙÊý¾Ý²éÒ»´Î
* @return boolean
**/
public boolean CustomersToRedis(String path,int stepSize) {
long zero = System.currentTimeMillis();

//Êý¾Ý×ÜÁ¿
int total = userDao.getCount();

if(total==0){
return false;
}
stepSize=stepSize==0?100000:stepSize;
//·Ö²½
int step =(total%stepSize)==0?(total/stepSize):(total/stepSize+1);
long startTime;//¿ªÊ¼Ê±¼ä
long selectTime;//²éѯ»¨·Ñʱ¼ä
long endTime;//½áÊøʱ¼ä
long saveTime;//´æÈëredis»¨·Ñʱ¼ä
long dis;

for(int j = 1;j<=step;j++){
System.out.println("######µÚ"+j+"´Î²Ù×÷######");
startTime= System.currentTimeMillis();
//²é³östepSizeÌõÊý¾Ý
List> res=userDao.getLimitUser((j-1)*stepSize,stepSize);

selectTime = System.currentTimeMillis();
System.out.println("²éѯ»¨·Ñʱ¼ä£º"+(selectTime-startTime)+"ms");

//½«ÕâЩÊý¾Ý´æÈëredis

handleSave(path, res);
saveTime = System.currentTimeMillis();
System.out.println("´æÈëredis»°·Ñʱ¼ä£º"+(saveTime-selectTime)+"ms");

endTime= System.currentTimeMillis();
dis= endTime-startTime;
System.out.println("ÿʮÍòÌõÊý¾Ý»¨·Ñʱ¼ä£º"+dis+"ms");
System.out.println();//»»ÐÐ
}

long last = System.currentTimeMillis();

System.out.println("×ܹ²»¨·Ñʱ¼ä"+(last-zero)+"ms");
return true;
}
//RedisTemplateʹÓÃPipeLine
//ʹÓùܵÀ´æ´¢
public void handleSave(String path,List> maps) {
List resultList = redisTemplate.executePipelined(new RedisCallback() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
String tempKey="";
for(Map map:maps){
tempKey=map.get("name")+"||"+map.get("sex")+"||"+map.get("hobby")+map.get("height");
connection.hSet(path.getBytes(),tempKey.getBytes(),redisTemplate.getValueSerializer().serialize(map));
}
return null;
}
});
System.out.println("³¤¶È£º"+resultList.size());
}
}

4.dao

package com.example.studyspringboot.studyboot.dao;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface UserDao {
//·ÖÒ³²éѯ
List> getLimitUser(@Param("st") int st, @Param("ed") int ed);

//×ÜÌõÊý
Integer getCount();
}

5.mapper/*.xml











²¿·Ö½á¹ûչʾ


redis´æ´¢½á¹û



ºǫ́´òÓ¡Êä³ö



¿ÉÒÔ¿´³ö²éѯ»¨·ÑÓÉÓÚÊDZ¾»úʱ¼ä»¹²»Ë㳤£¬Èç¹ûÊÇÔ¶³ÌÁ¬½Óʱ¼ä¿ÉÄÜÒª³¤µÃ¶à¡£


ÿ´Î´æ10Íò£¬100ÍòµÄÊý¾Ý´ó¸ÅÐèÒª56Ã룬Èç¹û²»ËãÁ´½Ó²éѯµÄʱ¼ä¿ÉÄÜ40Ãë¾Í¿ÉÒÔÁË¡£


6.Êý¾Ý±í


?


7.¹¤¾ßÀà

package com.example.studyspringboot.studyboot.utils.stuRedis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisUtil {
//ËøÃû³Æ
public static final String LOCK_PREFIX = "redis_lock";
//¼ÓËøʧЧʱ¼ä£¬ºÁÃë
public static final int LOCK_EXPIRE = 300; // ms

@Resource
private RedisTemplate redisTemplate;

public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
//=============================common============================

/**
* Ö¸¶¨»º´æʧЧʱ¼ä
*
* @param key ¼ü
* @param time ʱ¼ä(Ãë)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
log.info("Ö¸¶¨redis»º´æʧЧʱ¼ä³É¹¦£¬key:{},time:{}",key,time);
return true;
} catch (Exception e) {
e.printStackTrace();
log.info("Ö¸¶¨redis»º´æʧЧʱ¼äʧ°Ü£¬key:{},time:{}",key,time);
return false;
}
}

/**
* ¸ù¾Ýkey »ñÈ¡¹ýÆÚʱ¼ä
*
* @param key ¼ü ²»ÄÜΪnull
* @return ʱ¼ä(Ãë) ·µ»Ø0´ú±íΪÓÀ¾ÃÓÐЧ
*/
public long getExpire(String key) {
long time=redisTemplate.getExpire(key, TimeUnit.SECONDS);
log.info("¸ù¾Ýkey»ñÈ¡redis»º´æʧЧʱ¼ä£¬key:{},time:{}",key,time);
return time;
}

/**
* ÅжÏkeyÊÇ·ñ´æÔÚ
*
* @param key ¼ü
* @return true ´æÔÚ false²»´æÔÚ
*/
public boolean hasKey(String key) {
try {
boolean flag=redisTemplate.hasKey(key);
log.info("ÅжÏredisÊÇ·ñ°üº¬key£¬key:{},return:{}",key,flag);
return flag;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ɾ³ý»º´æ
*
* @param key ¿ÉÒÔ´«Ò»¸öÖµ »ò¶à¸ö
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
boolean flag=redisTemplate.delete(key[0]);
log.info("´Óredisɾ³ýkey£¬key:{},return:{}",key,flag);
} else {
long count=redisTemplate.delete(CollectionUtils.arrayToList(key));
log.info("´Óredisɾ³ýkey£¬key:{},return:{}",key,count);
}
}
}

//============================String=============================

/**
* ÆÕͨ»º´æ»ñÈ¡
*
* @param key ¼ü
* @return Öµ
*/
public Object get(String key) {
if(key==null) {
log.info("´Óredis»ñÈ¡key£¬key:{},return:{}",key,null);
return null;
}else {
Object object=redisTemplate.opsForValue().get(key);
log.info("´Óredis»ñÈ¡key£¬key:{},return:{}",key,object);
return object;
}
}

/**
* ÆÕͨ»º´æ·ÅÈë
*
* @param key ¼ü
* @param value Öµ
* @return true³É¹¦ falseʧ°Ü
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
log.info("±£´æ¼üÖµµ½redis³É¹¦£¬key:{},value:{}",key,value);
return true;
} catch (Exception e) {
e.printStackTrace();
log.info("±£´æ¼üÖµµ½redisʧ°Ü£¬key:{},value:{}",key,value);
return false;
}

}

/**
* ÆÕͨ»º´æ·ÅÈë²¢ÉèÖÃʱ¼ä
*
* @param key ¼ü
* @param value Öµ
* @param time ʱ¼ä(Ãë) timeÒª´óÓÚ0 Èç¹ûtimeСÓÚµÈÓÚ0 ½«ÉèÖÃÎÞÏÞÆÚ
* @return true³É¹¦ false ʧ°Ü
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
log.info("±£´æ¼üÖµµ½redis³É¹¦²¢É趨ʧЧʱ¼ä£¬key:{},value:{},time:{},",key,value,time);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}


//================================Map=================================

/**
* HashGet
*
* @param key ¼ü ²»ÄÜΪnull
* @param item Ïî ²»ÄÜΪnull
* @return Öµ
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}

/**
* »ñÈ¡hashKey¶ÔÓ¦µÄËùÓмüÖµ
*
* @param key ¼ü
* @return ¶ÔÓ¦µÄ¶à¸ö¼üÖµ
*/
public Map hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}

/**
* HashSet
*
* @param key ¼ü
* @param map ¶ÔÓ¦¶à¸ö¼üÖµ
* @return true ³É¹¦ false ʧ°Ü
*/
public boolean hmset(String key, Map map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* HashSet ²¢ÉèÖÃʱ¼ä
*
* @param key ¼ü
* @param map ¶ÔÓ¦¶à¸ö¼üÖµ
* @param time ʱ¼ä(Ãë)
* @return true³É¹¦ falseʧ°Ü
*/
public boolean hmset(String key, Map map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ÏòÒ»ÕÅhash±íÖзÅÈëÊý¾Ý,Èç¹û²»´æÔÚ½«´´½¨
*
* @param key ¼ü
* @param item Ïî
* @param value Öµ
* @return true ³É¹¦ falseʧ°Ü
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ÏòÒ»ÕÅhash±íÖзÅÈëÊý¾Ý,Èç¹û²»´æÔÚ½«´´½¨
*
* @param key ¼ü
* @param item Ïî
* @param value Öµ
* @param time ʱ¼ä(Ãë) ×¢Òâ:Èç¹ûÒÑ´æÔÚµÄhash±íÓÐʱ¼ä,ÕâÀォ»áÌæ»»Ô­ÓеÄʱ¼ä
* @return true ³É¹¦ falseʧ°Ü
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ɾ³ýhash±íÖеÄÖµ
*
* @param key ¼ü ²»ÄÜΪnull
* @param item Ïî ¿ÉÒÔʹ¶à¸ö ²»ÄÜΪnull
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}

/**
* ÅжÏhash±íÖÐÊÇ·ñÓиÃÏîµÄÖµ
*
* @param key ¼ü ²»ÄÜΪnull
* @param item Ïî ²»ÄÜΪnull
* @return true ´æÔÚ false²»´æÔÚ
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}


//============================set=============================

/**
* ¸ù¾Ýkey»ñÈ¡SetÖеÄËùÓÐÖµ
*
* @param key ¼ü
* @return
*/
public Set sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* ¸ù¾Ývalue´ÓÒ»¸ösetÖвéѯ,ÊÇ·ñ´æÔÚ
*
* @param key ¼ü
* @param value Öµ
* @return true ´æÔÚ false²»´æÔÚ
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ½«Êý¾Ý·ÅÈëset»º´æ
*
* @param key ¼ü
* @param values Öµ ¿ÉÒÔÊǶà¸ö
* @return ³É¹¦¸öÊý
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}

/**
* ½«setÊý¾Ý·ÅÈ뻺´æ
*
* @param key ¼ü
* @param time ʱ¼ä(Ãë)
* @param values Öµ ¿ÉÒÔÊǶà¸ö
* @return ³É¹¦¸öÊý
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}

/**
* »ñÈ¡set»º´æµÄ³¤¶È
*
* @param key ¼ü
* @return
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}

/**
* ÒƳýֵΪvalueµÄ
*
* @param key ¼ü
* @param values Öµ ¿ÉÒÔÊǶà¸ö
* @return ÒƳýµÄ¸öÊý
*/
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
//===============================list=================================

/**
* »ñÈ¡list»º´æµÄÄÚÈÝ
*
* @param key ¼ü
* @param start ¿ªÊ¼
* @param end ½áÊø 0 µ½ -1´ú±íËùÓÐÖµ
* @return
*/
public List lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* »ñÈ¡list»º´æµÄ³¤¶È
*
* @param key ¼ü
* @return
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}

/**
* ͨ¹ýË÷Òý »ñÈ¡listÖеÄÖµ
*
* @param key ¼ü
* @param index Ë÷Òý index>=0ʱ£¬ 0 ±íÍ·£¬1 µÚ¶þ¸öÔªËØ£¬ÒÀ´ÎÀàÍÆ£»index<0ʱ£¬-1£¬±í⣬-2µ¹ÊýµÚ¶þ¸öÔªËØ£¬ÒÀ´ÎÀàÍÆ
* @return
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* ½«list·ÅÈ뻺´æ
*
* @param key ¼ü
* @param value Öµ
* @param time ʱ¼ä(Ãë)
* @return
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ½«list·ÅÈ뻺´æ
*
* @param key ¼ü
* @param value Öµ
* @param time ʱ¼ä(Ãë)
* @return
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ½«list·ÅÈ뻺´æ
*
* @param key ¼ü
* @param value Öµ
* @param time ʱ¼ä(Ãë)
* @return
*/
public boolean lSet(String key, List value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ½«list·ÅÈ뻺´æ
*
* @param key ¼ü
* @param value Öµ
* @param time ʱ¼ä(Ãë)
* @return
*/
public boolean lSet(String key, List value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ¸ù¾ÝË÷ÒýÐÞ¸ÄlistÖеÄijÌõÊý¾Ý
*
* @param key ¼ü
* @param index Ë÷Òý
* @param value Öµ
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

/**
* ÒƳýN¸öֵΪvalue
*
* @param key ¼ü
* @param count ÒƳý¶àÉÙ¸ö
* @param value Öµ
* @return ÒƳýµÄ¸öÊý
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}

/**
* ·Ö²¼Ê½Ëø
*
* @param key keyÖµ
* @return ÊÇ·ñ»ñÈ¡µ½
*/
@SuppressWarnings("unchecked")
public boolean lock(String key){
String lock = LOCK_PREFIX + key;
return (Boolean) redisTemplate.execute((RedisCallback) connection -> {

long expireAt = System.currentTimeMillis() + LOCK_EXPIRE + 1;
Boolean acquire = connection.setNX(lock.getBytes(), String.valueOf(expireAt).getBytes());


if (acquire) {
return true;
} else {

byte[] value = connection.get(lock.getBytes());

if (Objects.nonNull(value) && value.length > 0) {

long expireTime = Long.parseLong(new String(value));
// Èç¹ûËøÒѾ­¹ýÆÚ
if (expireTime < System.currentTimeMillis()) {
// ÖØмÓËø£¬·ÀÖ¹ËÀËø
byte[] oldValue = connection.getSet(lock.getBytes(), String.valueOf(System.currentTimeMillis() + LOCK_EXPIRE + 1).getBytes());
return Long.parseLong(new String(oldValue)) < System.currentTimeMillis();
}
}
}
return false;
});
}

/**
* ɾ³ýËø
*
* @param key
*/
public void delete(String key) {
redisTemplate.delete(key);
}
}
8.Ò»´Î²éѯ£¬·ÖÅú²åÈëµÄЧÂʶԱÈ

ºËÐÄ´úÂë


@Test
public void method3() {


long startTime = System.currentTimeMillis();
int total = userDao.getCount();
List> resList = userDao.getLimitUser(0,total);
long selectTime = System.currentTimeMillis();
System.out.println("²éѯÊý¾Ý»¨·Ñʱ¼ä£º"+(selectTime-startTime)+"ms");
int len = resList.size();//ʵ¼ÊÊý¾Ý×ÜÁ¿(µÈÓÚ)total
int stepSize=100000;//²½³¤Îª10Íò£»
String path="nbs:hello:leaf";
int cnt=1;
long startRedisTime;
long endRedisTime= -System.currentTimeMillis();
List> tempList = new ArrayList<>();
for(int i=0;i tempList.add(resList.get(i));
if(i%stepSize==0&&i!=0){
System.out.println("µÚ"+cnt+"´ÎÏòredisÖдæÈëÊý¾Ý");
startRedisTime = System.currentTimeMillis();

handleSave(path,tempList);
tempList.clear();//Çå³ýÄÚ´æ
cnt++;
endRedisTime = System.currentTimeMillis();
System.out.println("´æÈëredi»¨·Ñʱ¼ä£º"+( endRedisTime-startRedisTime)+"ms");
}
}
//´æÔÚ²»ÄÜÇ¡ºÃÕû³ýµÄ£¬ÐèÒª¶îÍâ´¦Àí
if(tempList.size()>0){
startRedisTime = System.currentTimeMillis();
handleSave(path,tempList);
endRedisTime = System.currentTimeMillis();
tempList.clear();//Çå³ýÄÚ´æ
System.out.println("´æÈëredi»¨·Ñʱ¼ä£º"+( endRedisTime-startRedisTime)+"ms");
}

System.out.println("×ܹ²»¨·ÑµÄʱ¼äΪ£º"+(endRedisTime-startTime)+"ms");

}

ÔËÐнá¹û


²éѯÊý¾Ý»¨·Ñʱ¼ä£º17521ms
µÚ1´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100001
´æÈëredi»¨·Ñʱ¼ä£º6233ms
µÚ2´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º4107ms
µÚ3´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º3579ms
µÚ4´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º3406ms
µÚ5´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º3384ms
µÚ6´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º3468ms
µÚ7´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º3382ms
µÚ8´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º3359ms
µÚ9´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º3742ms
µÚ10´ÎÏòredisÖдæÈëÊý¾Ý
³¤¶È£º100000
´æÈëredi»¨·Ñʱ¼ä£º4318ms
³¤¶È£º24
´æÈëredi»¨·Ñʱ¼ä£º6ms
×ܹ²»¨·ÑµÄʱ¼äΪ£º56535ms

¿ÉÄÜÓÉÓÚ¶¼ÊDZ¾µØÁ¬½Ó£¬¶ø²»ÊÇÔ¶³ÌÁ¬½Ó¡£ËùÒÔ¿´²»³öÀ´²î±ð¡£


£¨1£©ÖµµÃ×¢ÒâµÄÊÇ´óÁ¿Êý¾ÝÈç¹ûÒ»´ÎÐÔ´ÓÊý¾Ý¿â¶Áµ½ÄÚ´æÖУ¬²Ù×÷ÆðÀ´³ÌÐò¾Í»á±ÀÀ£¡£±ÈÈçÒ»´ÎÐÔ¶ÁÈë300ÍòµÄÊý¾Ý¾ÍÎÞ·¨¼ÌÐø½øÐд洢²Ù×÷ÁË¡£ËùÒÔÕâ¸öÒ»´ÎÐÔ¶ÁÈ룬·ÖÅú´æµÄÏë·¨¾­¹ý²âÊÔÊÇÐв»Í¨µÄ¡£


£¨2£©ÁíÍâËæ×ÅÊý¾ÝÁ¿µÄÔö¼Ó£¬¼´±ãÊÇ·ÖÅú´ÓÊý¾Ý¿â¶Á£¬ÔÙ·ÖÅúддÈëredisÖУ¬Ð§ÂÊÒ²Ô½À´Ò²µÍ¡£100ÍòÊý¾ÝÐèÒª1·ÖÖÓ¡£


? 300°ÙÍòÊý¾ÝÈ´Òª4·ÖÖÓ¡£Èç¹ûÊý¾Ý½øÒ»²½Ôö¼ÓµÄ»°¿ÉÄܾÍÎÞ·¨Ê¤ÈÎÁË¡£¾ßÌåÈçÏ£º



相关推荐


友情链接: