From 7ca49e9559d2c0ef87187b8a8eba1362e4853e2e Mon Sep 17 00:00:00 2001 From: qiurunze Date: Mon, 24 Dec 2018 14:59:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=BD=91=E7=AB=99=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- docs/redis-lua.md | 6 ++ .../miaosha/controller/LoginController.java | 6 +- .../miaosha/redis/redismanager/RedisLua.java | 58 +++++++++++++++++++ .../miaosha/redis/redismanager/lua/test.lua | 5 ++ .../redis/redismanager/lua/testlua.lua | 17 +----- src/main/resources/application.properties | 4 +- src/main/resources/templates/login.html | 3 + 8 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 docs/redis-lua.md create mode 100644 src/main/java/com/geekq/miaosha/redis/redismanager/lua/test.lua diff --git a/README.md b/README.md index ae6dc39..32dd343 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ #### [分布式治理框架-dubbo - zk - 解析--未更新](/docs/redis-code.md) #### [通信mq-Kafka--未更新](/docs/redis-code.md) #### [微服务框架--未更新](/docs/redis-code.md) -#### [JVM内存解析与性能调优--未更新](/docs/redis-code.md) +#### [lua学习](/docs/redis-lua.md) #### [面试专题(最后更新)--未更新](/docs/code-solve.md) diff --git a/docs/redis-lua.md b/docs/redis-lua.md new file mode 100644 index 0000000..073cc18 --- /dev/null +++ b/docs/redis-lua.md @@ -0,0 +1,6 @@ +### lua学习 + + 有问题或者宝贵意见联系我的QQ,非常希望你的加入! + +>数据类型 + \ No newline at end of file diff --git a/src/main/java/com/geekq/miaosha/controller/LoginController.java b/src/main/java/com/geekq/miaosha/controller/LoginController.java index a53fda0..8f6a754 100644 --- a/src/main/java/com/geekq/miaosha/controller/LoginController.java +++ b/src/main/java/com/geekq/miaosha/controller/LoginController.java @@ -8,6 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -24,8 +25,11 @@ public class LoginController { private MiaoShaUserService userService; @RequestMapping("/to_login") - public String tologin(LoginVo loginVo) { + public String tologin(LoginVo loginVo, Model model) { logger.info(loginVo.toString()); + String count = RedisLua.getVistorCount().toString(); + logger.info("访问网站的次数为:{}",count); + model.addAttribute("count",count); return "login"; } diff --git a/src/main/java/com/geekq/miaosha/redis/redismanager/RedisLua.java b/src/main/java/com/geekq/miaosha/redis/redismanager/RedisLua.java index 08fb5be..58ca777 100644 --- a/src/main/java/com/geekq/miaosha/redis/redismanager/RedisLua.java +++ b/src/main/java/com/geekq/miaosha/redis/redismanager/RedisLua.java @@ -1,5 +1,8 @@ package com.geekq.miaosha.redis.redismanager; +import com.geekq.miaosha.controller.LoginController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import java.util.ArrayList; @@ -10,6 +13,8 @@ import java.util.List; */ public class RedisLua { + private static Logger logger = LoggerFactory.getLogger(RedisLua.class); + /** * 未完成 需 evalsha更方便 限制ip 或者 手机号访问次数 */ @@ -39,4 +44,57 @@ public class RedisLua { Object object = jedis.evalsha(luaScript, keys, argves); System.out.println(object); } + + /** + * 统计访问次数 + */ + public static Object getVistorCount() { + + Jedis jedis = null; + Object object = null; + try { + jedis = RedisManager.getJedis(); + + String count = + "local num=redis.call('get',KEYS[1]) return num"; + List keys = new ArrayList(); + keys.add("count:login"); + List argves = new ArrayList(); + jedis.auth("youxin11"); + String luaScript = jedis.scriptLoad(count); + System.out.println(luaScript); + object = jedis.evalsha(luaScript, keys, argves); + } catch (Exception e) { + logger.error("统计访问次数失败!!!",e); + return "0"; + } + return object; + } + + /** + * 统计访问次数 + */ + public static void vistorCount() { + + Jedis jedis = null; + Object object = null; + try { + jedis = RedisManager.getJedis(); + String count = + "local num=redis.call('incr',KEYS[1]) return num"; + List keys = new ArrayList(); + keys.add("count:login"); + List argves = new ArrayList(); + jedis.auth("youxin11"); + String luaScript = jedis.scriptLoad(count); + System.out.println(luaScript); + jedis.evalsha(luaScript, keys, argves); + } catch (Exception e) { + logger.error("统计访问次数失败!!!",e); + } + } + public static void main(String[] args) { + getVistorCount(); + } + } diff --git a/src/main/java/com/geekq/miaosha/redis/redismanager/lua/test.lua b/src/main/java/com/geekq/miaosha/redis/redismanager/lua/test.lua new file mode 100644 index 0000000..376c501 --- /dev/null +++ b/src/main/java/com/geekq/miaosha/redis/redismanager/lua/test.lua @@ -0,0 +1,5 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by qiurunze. +--- DateTime: 2018/12/24 13:22 +--- diff --git a/src/main/java/com/geekq/miaosha/redis/redismanager/lua/testlua.lua b/src/main/java/com/geekq/miaosha/redis/redismanager/lua/testlua.lua index a0ca59c..3b8a67b 100644 --- a/src/main/java/com/geekq/miaosha/redis/redismanager/lua/testlua.lua +++ b/src/main/java/com/geekq/miaosha/redis/redismanager/lua/testlua.lua @@ -4,20 +4,7 @@ --- DateTime: 2018/12/22 16:33 --- --- -local key = KEY[1] -local content = KEY[2] -local ttl = AVG[1] -local lockSet = redis.call('setnx',key,content) -if lockSet==1 then - redis.call('pexpire',key,ttl) -else - local value = redis.call('get',key) - if value==content then - lockSet=1 - redis.call('pexpire',key,ttl) - end -end -return lockSet - + +print(10) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 26143c0..8accc2e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -20,7 +20,7 @@ mybatis.config-locations=classpath:mybatis/config.xml #datasource spring.datasource.url=jdbc:mysql://39.107.245.253:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false spring.datasource.username=root -spring.datasource.password=xxxxx +spring.datasource.password=nihaoma spring.datasource.driver-class-name=com.mysql.jdbc.Driver #druid spring.datasource.type=com.alibaba.druid.pool.DruidDataSource @@ -49,7 +49,7 @@ spring.resources.static-locations=classpath:/static/ redis.host=39.107.245.253 redis.port=6379 redis.timeout=100 -redis.password=xxxxx +redis.password=youxin11 redis.poolMaxTotal=1000 redis.poolMaxIdle=500 redis.poolMaxWait=500 diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index b9b245a..882b189 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -53,6 +53,9 @@
+
+

+