提交网站统计

This commit is contained in:
qiurunze 2018-12-24 14:59:33 +08:00
parent a97e9dbc23
commit 7ca49e9559
8 changed files with 82 additions and 19 deletions

View File

@ -76,7 +76,7 @@
#### [分布式治理框架-dubbo - zk - 解析--未更新](/docs/redis-code.md) #### [分布式治理框架-dubbo - zk - 解析--未更新](/docs/redis-code.md)
#### [通信mq-Kafka--未更新](/docs/redis-code.md) #### [通信mq-Kafka--未更新](/docs/redis-code.md)
#### [微服务框架--未更新](/docs/redis-code.md) #### [微服务框架--未更新](/docs/redis-code.md)
#### [JVM内存解析与性能调优--未更新](/docs/redis-code.md) #### [lua学习](/docs/redis-lua.md)
#### [面试专题(最后更新)--未更新](/docs/code-solve.md) #### [面试专题(最后更新)--未更新](/docs/code-solve.md)

6
docs/redis-lua.md Normal file
View File

@ -0,0 +1,6 @@
### lua学习
有问题或者宝贵意见联系我的QQ,非常希望你的加入!
>数据类型

View File

@ -8,6 +8,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@ -24,8 +25,11 @@ public class LoginController {
private MiaoShaUserService userService; private MiaoShaUserService userService;
@RequestMapping("/to_login") @RequestMapping("/to_login")
public String tologin(LoginVo loginVo) { public String tologin(LoginVo loginVo, Model model) {
logger.info(loginVo.toString()); logger.info(loginVo.toString());
String count = RedisLua.getVistorCount().toString();
logger.info("访问网站的次数为:{}",count);
model.addAttribute("count",count);
return "login"; return "login";
} }

View File

@ -1,5 +1,8 @@
package com.geekq.miaosha.redis.redismanager; 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 redis.clients.jedis.Jedis;
import java.util.ArrayList; import java.util.ArrayList;
@ -10,6 +13,8 @@ import java.util.List;
*/ */
public class RedisLua { public class RedisLua {
private static Logger logger = LoggerFactory.getLogger(RedisLua.class);
/** /**
* 未完成 evalsha更方便 限制ip 或者 手机号访问次数 * 未完成 evalsha更方便 限制ip 或者 手机号访问次数
*/ */
@ -39,4 +44,57 @@ public class RedisLua {
Object object = jedis.evalsha(luaScript, keys, argves); Object object = jedis.evalsha(luaScript, keys, argves);
System.out.println(object); 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<String> keys = new ArrayList<String>();
keys.add("count:login");
List<String> argves = new ArrayList<String>();
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<String> keys = new ArrayList<String>();
keys.add("count:login");
List<String> argves = new ArrayList<String>();
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();
}
} }

View File

@ -0,0 +1,5 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by qiurunze.
--- DateTime: 2018/12/24 13:22
---

View File

@ -4,20 +4,7 @@
--- DateTime: 2018/12/22 16:33 --- DateTime: 2018/12/22 16:33
--- ---
--- ---
local key = KEY[1]
local content = KEY[2] print(10)
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

View File

@ -20,7 +20,7 @@ mybatis.config-locations=classpath:mybatis/config.xml
#datasource #datasource
spring.datasource.url=jdbc:mysql://39.107.245.253:3306/miaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false 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.username=root
spring.datasource.password=xxxxx spring.datasource.password=nihaoma
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#druid #druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 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.host=39.107.245.253
redis.port=6379 redis.port=6379
redis.timeout=100 redis.timeout=100
redis.password=xxxxx redis.password=youxin11
redis.poolMaxTotal=1000 redis.poolMaxTotal=1000
redis.poolMaxIdle=500 redis.poolMaxIdle=500
redis.poolMaxWait=500 redis.poolMaxWait=500

View File

@ -53,6 +53,9 @@
<div class="col-md-5"> <div class="col-md-5">
<button class="btn btn-primary btn-block" type="submit" onclick="login()">登录</button> <button class="btn btn-primary btn-block" type="submit" onclick="login()">登录</button>
</div> </div>
<div class="col-md-5">
<p th:text="'网站访问次数为:'+${count}" ></p>
</div>
</div> </div>
</form> </form>