這篇文章將為大家詳細(xì)講解有關(guān)redis制作計(jì)數(shù)器的方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司主營嘉定網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),嘉定h5小程序開發(fā)搭建,嘉定網(wǎng)站營銷推廣歡迎嘉定等地區(qū)企業(yè)咨詢
用redis來存儲下單計(jì)數(shù),每天當(dāng)?shù)谝粋€(gè)用戶下單時(shí),在redis中創(chuàng)建兩個(gè)map對象,20170314_pz_ip_order_count 用來儲存20170314 這天的ip 下單計(jì)數(shù),map的key是ip地址,20170314_pz_user_order_count 用來存儲20170314這天的用戶下單計(jì)數(shù) map對象的key是用戶id.
1.redis依賴包
org.springframework.data
spring-data-redis
1.4.0.RELEASE
redis.clients
jedis
2.5.1
ps:用于開始導(dǎo)入的clieant的jar包1.4 版本比較低,所以導(dǎo)致redis連不上,所以redis還是要看服務(wù)端的版本,選擇對應(yīng)的client
2.spring配置
ps:高版本的poolConfig配置屬性有所變更,比如之前maxAcive 變?yōu)?maxTotal
3.編碼
package com.pz998.rpc.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
public abstract class AbstractBaseRedisService
@Autowired
protected RedisTemplate
/**
* 設(shè)置redisTemplate
* @param redisTemplate the redisTemplate to set
*/
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 獲取 RedisSerializer
*
------------------------------
*/
protected RedisSerializer getRedisSerializer() {
return redisTemplate.getStringSerializer();
}
}
package com.pz998.rpc.service.impl;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import com.pz998.rpc.common.utils.Contents;
import com.pz998.rpc.service.RequestCountRpcService;
@Service("requestCountRpcService")
public class RequestCountRpcServiceImpl extends AbstractBaseRedisService
public void ipCount(String ip, Integer count) {
String curDate = DateFormatUtils.format(new Date(), "yyyyMMdd");
String key = curDate+Contents.IP_LIMIT_KEY_SUFFIX;
Integer oldCount = get(key,ip);
oldCount = oldCount+count;
add(key, ip,oldCount);
}
public void userCount(String userId, Integer count) {
String curDate = DateFormatUtils.format(new Date(), "yyyyMMdd");
String key = curDate+Contents.USER_LIMIT_KEY_SUFFIX;
Integer oldCount = get(key,userId);
oldCount = oldCount+count;
add(key,userId,oldCount);
}
public void clear(String key, String hashKey){
redisTemplate.opsForHash().delete(key, hashKey);
}
public void add(String key, String hashKey, Integer count) {
String countStr = count==null?"0":count.toString();
redisTemplate.opsForHash().put(key,hashKey, countStr);
redisTemplate.expire(key, 24*60*2, TimeUnit.MINUTES);
}
public Integer get(String key, String hashKey) {
String val = (String)redisTemplate.opsForHash().get(key, hashKey);
if(StringUtils.isEmpty(val)){
return 0;
}
return Integer.parseInt(val);
}
}
4.攔截器配置
submitJjqhOrder
submitYymyOrder
submitJypzOrder
submitGhpzOrder
submitDqbgOrder
ps:將具體攔截的業(yè)務(wù)方法methodSet,ip限制的閥值ipLimitCount,user閥值userLimitCount 作為攔截器的屬性進(jìn)行注入,這樣增強(qiáng)了配置靈活性
5.攔截器編寫
package com.pz998.app.service.interceptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.pz998.rpc.common.model.vo.ResponseVoRpc;
import com.pz998.rpc.common.utils.Contents;
import com.pz998.rpc.service.RequestCountRpcService;
public class SecurityInterceptor implements HandlerInterceptor{
private RequestCountRpcService requestCountRpcService;
private Integer ipLimitCount = 10;
private Integer userLimitCount = 5;
private Set methodSet;
public String getRemoteHost(javax.servlet.http.HttpServletRequest request){
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getRemoteAddr();
}
return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String customerId = request.getParameter("customerId");
String mn = request.getParameter("mn");
if(StringUtils.isEmpty(mn)){
return true;
}
if(!methodSet.contains(mn)){
return true;
}
ServletContext servletContext = request.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
requestCountRpcService = (RequestCountRpcService)ctx.getBean("requestCountRpcService" );
String host = getRemoteHost(request);
ResponseVoRpc responseVoRpc = new ResponseVoRpc();
if(isLimitIp(host)){
PrintWriter out = null;
try {
out = response.getWriter();
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
responseVoRpc.setCode(ResponseVoRpc.CODE_COMMON_FAILED);
responseVoRpc.setCodeMsg("同一ip下單過多,已被限制");
out.write(JSON.toJSONString(responseVoRpc));
} catch (IOException e) {
e.printStackTrace();
}finally{
if(out != null)
out.close();
}
return false;
}
if(isLimitUser(customerId)){
PrintWriter out = null;
try {
out = response.getWriter();
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
responseVoRpc.setCode(ResponseVoRpc.CODE_COMMON_FAILED);
responseVoRpc.setCodeMsg("同一用戶下單過多,已被限制");
out.write(JSON.toJSONString(responseVoRpc));
} catch (IOException e) {
e.printStackTrace();
}finally{
if(out != null)
out.close();
}
return false;
}
requestCountRpcService.ipCount(host, 1);
requestCountRpcService.userCount(customerId, 1);
return true;
}
private boolean isLimitUser(String urseId) {
String key = DateFormatUtils.format(new Date(), "yyyyMMdd")+Contents.USER_LIMIT_KEY_SUFFIX;
Integer count = requestCountRpcService.get(key, urseId);
if(count>=userLimitCount){
return true;
}
return false;
}
private boolean isLimitIp(String host) {
String key = DateFormatUtils.format(new Date(), "yyyyMMdd")+Contents.IP_LIMIT_KEY_SUFFIX;
Integer count = requestCountRpcService.get(key, host);
if(count>=ipLimitCount){
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
public Integer getIpLimitCount() {
return ipLimitCount;
}
public void setIpLimitCount(Integer ipLimitCount) {
this.ipLimitCount = ipLimitCount;
}
public Integer getUserLimitCount() {
return userLimitCount;
}
public void setUserLimitCount(Integer userLimitCount) {
this.userLimitCount = userLimitCount;
}
public Set getMethodSet() {
return methodSet;
}
public void setMethodSet(Set methodSet) {
this.methodSet = methodSet;
}
}
以上便是redis制作計(jì)數(shù)器的方法,雖然從篇幅上看很復(fù)雜,但是示例代碼非常詳細(xì)且容易理解,如果想了解更多相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊。