001.Redis分布式锁应用(一天中的第一次访问是 “你好,主人!” 其余访问是 “你好,请下次再来!”)

 

 

 

 

package com.redis.subject.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisUtils
{
    private static final  JedisPool JEDIS_POOL;

    static
    {
        /**
         * 1.getResourceAsStream()从resource目录读取文件变成输入流
         * 2. prop.load()将输入流中转化复制到Properties中
         */
        InputStream resourceAsStream = JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        Properties pro = new Properties();
        try
        {
            pro.load(resourceAsStream);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        JedisPoolConfig config =new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
        JEDIS_POOL =new JedisPool(config,pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));
    }

    public static Jedis getJedis()
    {
        return JEDIS_POOL.getResource();
    }

    public static void closeJedis(Jedis jedis) {
        if (jedis != null) {
            jedis.close();

        }
    }
}
package com.redis.subject.util;

import java.util.Calendar;

public class SetUpSecondUtils
{
    /**
     * @return  当前时间到凌晨12点剩余秒数
     */
    public static long setUpSecond()
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long seconds = (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
        return seconds;
    }
}
package com.redis.subject.one;


import com.redis.subject.util.JedisUtils;
import com.redis.subject.util.SetUpSecondUtils;
import redis.clients.jedis.Jedis;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/helloServlet")
public class HelloServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Jedis jedis = JedisUtils.getJedis();
        String sign = jedis.set("visit", "tourist", "NX", "EX", SetUpSecondUtils.setUpSecond());
        JedisUtils.closeJedis(jedis);
        if (sign != null)
        {
            resp.getWriter().write("你好,主人!");
        }
        else
        {
            resp.getWriter().write("你好,请下次再来!");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        doGet(req,resp);
    }
}

 

posted @ 2021-11-04 22:09  李林林  阅读(74)  评论(0编辑  收藏  举报