SpringMVC下利用Redis实现点赞踩功能

实现流程:

建立RedisUtil工具类

/**
*根据文章id 和 文章类型拼接成字符串作为Key存入redis缓存中
*可以根据实际修改
*
/
public class RedisKeyUtil {
    private static String SPLIT = ":";
    private static String BIZ_LIKE = "LIKE";
    private static String BIZ_DISLIKE = "DISLIKE";

    public static String getLikeKey(int entityId, int entityType) {
        return BIZ_LIKE + SPLIT + String.valueOf(entityType) + SPLIT + String.valueOf(entityId);
    }

    public static String getDisLikeKey(int entityId, int entityType) {
        return BIZ_DISLIKE + SPLIT + String.valueOf(entityType) + SPLIT + String.valueOf(entityId);
    }
}

建立Redis连接池

public class RedisPool {
    private static JedisPool pool;//jedis连接池
    private static Integer maxTotal = 20;// 最大连接数
    private static Integer maxIdle = 10;//在jedispool中最大的idle状态(空闲的)的jedis实例的个数
    private static Integer minIdle = 2; //在jedispool中最小的idle状态(空闲的)的jedis实例的个数

    private static Boolean testOnBorrow = "true"; //在borrow一个jedis实例的时候,是否要进行验证操作,如果赋值true,则得到的jedis实例肯定是可以用的
    private static Boolean testOnReturen = "true"; //在return一个jedis实例的时候,是否要进行验证操作,如果赋值true,则放回jedispool的jedis实例肯定是可以用的

    private static String redisip = redis.ip;//自己本机的redis IP地址也可以是服务器redis的ip地址
    private static Integer redisPort = redis.port;//自己本机的redis端口也可以是服务器所在的redis端口




    private static void initPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturen);

        config.setBlockWhenExhausted(true);//链接耗尽的时候,是否阻塞,false会抛出异常,true阻塞直到超时.默认为true

        pool = new JedisPool(config, redisip, redisPort, 1000 * 2);
    }

    static {
        initPool();
    }

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

    public static void returnBrokenResource(Jedis jedis) {
        pool.returnBrokenResource(jedis);
    }

    public static void returnResource(Jedis jedis) {
        pool.returnResource(jedis);
//        pool.close();
    }

    public static void main(String[] args) {
        Jedis jedis = pool.getResource();
        jedis.set("key", "value");
        returnResource(jedis);

        pool.destroy();//临时调用,销毁连接池中的所有链接
        System.out.println("program is end");
    }
}

建立RedisRoolUtil连接池工具类

public class RedisPoolUtil {
//添加一个键值对进入缓存,返回值被添加到集合中的新元素的数量,如果重复返回0
//
 public static long sadd(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = RedisPool.getJedis();
            return jedis.sadd(key, value);
        } catch (Exception e) {
            log.error("发生异常" + e.getMessage());
            RedisPool.returnBrokenResource(jedis);
            return 0;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
	//删除缓存中的一个键值对,返回值被成功移除的元素的数量,如果没有就返回0
	//
    public static long srem(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = RedisPool.getJedis();
            return jedis.srem(key, value);
        } catch (Exception e) {
            log.error("发生异常" + e.getMessage());
            RedisPool.returnBrokenResource(jedis);
            return 0;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
	//判断缓存中是否存在键值对,如果存在返回1,key不存在或值不存在返回0
	//
    public static boolean sismember(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = RedisPool.getJedis();
            return jedis.sismember(key, value);
        } catch (Exception e) {
            log.error("发生异常" + e.getMessage());
            RedisPool.returnBrokenResource(jedis);
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

	//返回缓存所在键所有的成员
    public static Set<String> smembers(String key) {
        Jedis jedis = null;
        Set<String> set = null;
        try {
            jedis = RedisPool.getJedis();
            set = jedis.smembers(key);
            return set;
        } catch (Exception e) {
            log.error("发生异常" + e.getMessage());
            RedisPool.returnBrokenResource(jedis);
            return set;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    //返回缓存所在键中的元素个数
	//
    public static long scard(String key) {
        Jedis jedis = null;
        try {
            jedis = RedisPool.getJedis();
            jedis.select(Const.REDIS_LIKE);
            return jedis.scard(key);
        } catch (Exception e) {
            log.error("发生异常" + e.getMessage());
            RedisPool.returnBrokenResource(jedis);
            return 0;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}

点赞与踩的具体小实现

 /**
     * 判断用户的喜欢状态
     * 如果喜欢返回1,如果不喜欢返回-1,否则返回0
     *
     * @param userId
     * @param entityType
     * @param entityId
     * @return
     */
    public int getLikeStatus(int userId, int entityType, int entityId) {
        //表示用户喜欢的key
        String likeKey = RedisKeyUtil.getLikeKey(entityId, entityType);
        if (RedisPoolUtil.sismember(likeKey, String.valueOf(userId))) {
            return 1;
        }
        String dislikeKey = RedisKeyUtil.getDisLikeKey(entityId, entityType);
        return RedisPoolUtil.sismember(dislikeKey, String.valueOf(userId)) ? -1 : 0;
    }

    /**
     * 如果用户喜欢就加1,然后从不喜欢减1,最后返回集合总数
     *
     * @param userId
     * @param entityType
     * @param entityId
     * @return
     */
    public long like(int userId, int entityType, int entityId) {
        String likeKey = RedisKeyUtil.getLikeKey(entityId, entityType);
        RedisPoolUtil.sadd(likeKey, String.valueOf(userId));

        String dislikeKey = RedisKeyUtil.getDisLikeKey(entityId, entityType);
        RedisPoolUtil.srem(dislikeKey, String.valueOf(userId));

        return RedisPoolUtil.scard(likeKey);
    }

    /**
     * 如果用户不喜欢就加1,然后从喜欢就减1,最后返回集合总数
     *
     * @param userId
     * @param entityType
     * @param entityId
     * @return
     */
    public long dislike(int userId, int entityType, int entityId) {

        String dislikeKey = RedisKeyUtil.getDisLikeKey(entityId, entityType);
        RedisPoolUtil.sadd(dislikeKey, String.valueOf(userId));

        String likeKey = RedisKeyUtil.getLikeKey(entityId, entityType);
        RedisPoolUtil.srem(likeKey, String.valueOf(userId));

        return RedisPoolUtil.scard(dislikeKey);
    }

大致的实现流程如上,思想不一定是完美,可能有其他更好的想法与做法希望能一起交流

至于赞踩数据库的设计,我也没有好的想法不过有个链接里面讨论的很有感触

链接:https://bbs.csdn.net/topics/391001223

Redis的一些用途可以借鉴借鉴

 

阿里服务器,新用户(可找未注册的手机)一年最低102,https://www.aliyun.com/minisite/goods?userCode=k84mxnzl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值