In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Java operation redis how to use expire to simulate how to limit the number of ip visits within a specified period of time, many beginners are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
First add the maven dependency and use JUinit to do the unit test.
Redis.clients jedis 2.7.0 junit junit 3.8.1 test
The redisutil class, which creates a thread pool that returns redis connection resources and frees resources
/ * redis utility class, get a link resource from the redis link pool * @ author Hades * time: December 14, 2015 * / public class RedisUtils {/ / define connection pool public static JedisPool pool = null / * get link resources * @ return * / public static synchronized Jedis getJedis () {if (pool==null) {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig (); jedisPoolConfig.setMaxTotal (100); / / maximum number of connections jedisPoolConfig.setMaxIdle (10) JedisPoolConfig.setMaxWaitMillis (1000); / / similar to timeout jedisPoolConfig.setTestOnBorrow (true); pool = new JedisPool (jedisPoolConfig, "192.168.57.133", 6379); / / create connection pool} Jedis jedis = pool.getResource (); return jedis } / * release linked resources * @ param jedis * / public static void returnJedis (Jedis jedis) {pool.returnResourceObject (jedis);}
Redis client class, using juinit for unit test.
/ * redis test class * @ author Hades * * / public class RedisTest {static Jedis jedis = RedisUtils.getJedis (); @ Test public void test3 () throws Exception {String ip = "192.168.57.2"; / / visited ip / / Test for (int I = 0; I
< 20; i++) { boolean flag = testLogin(ip); System.out.println(flag); } } /** * 模拟限制ip指定时间段内访问次数 * @param ip * @return */ public boolean testLogin(String ip) { String value = jedis.get(ip); if(value==null){ jedis.set(ip, "1"); jedis.expire(ip, 60);//设置过期时间60秒 return true; }else{ int parseInt = Integer.parseInt(value); //60秒内访问超过10次,就禁止访问 if(parseInt>10) {System.out.println ("access restricted!!") ; return false;} jedis.incr (ip);} return true } / * it takes time to insert 10, 000 pieces of data into jedis without pipes: 3184 * / @ Test public void test2 () throws Exception {/ / TODO Auto-generated method stub long start = System.currentTimeMillis (); for (int I = 0; I < 10000) Jedis.set ("a" + I, I + "); jedis.expire (" a "+ I, 60);} System.out.println (System.currentTimeMillis ()-start) } / * time required to import data in bulk using pipe commands: 204 * @ throws Exception * / @ Test public void test4 () throws Exception {long start = System.currentTimeMillis (); Pipeline pipelined = jedis.pipelined (); for (int I = 0; I < 10000) ) {pipelined.set ("a" + I, I + "); pipelined.expire (" a "+ I, 60);} pipelined.sync (); System.out.println (System.currentTimeMillis ()-start);}} will it help you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.