SpringBoot整合redis中setBit,getBit進行點贊


redis中的工具類

package com.showy.bbs.custom;

import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;

import static java.util.Objects.hash;

/**
* @author  
* @Date 2021-03-02
*/
public class RedisTemplateUtil {
    private static  StringRedisTemplate stringRedisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
    /**
     * 將指定param的值設置為1,{@param param}會經過hash計算進行存儲。
     *
     * @param key   bitmap結構的key
     * @param param 要設置偏移的key,該key會經過hash運算。
     * @param value true:即該位設置為1,否則設置為0
     * @return 返回設置該value之前的值。
     */
    public static Boolean setBit(String key, String param, boolean value) {
        return stringRedisTemplate.opsForValue().setBit(key, hash(param), value);
    }

    /**
     * 將指定param的值設置為0,{@param param}會經過hash計算進行存儲。
     *
     * @param key   bitmap結構的key
     * @param param 要移除偏移的key,該key會經過hash運算。
     * @return 若偏移位上的值為1,那么返回true。
     */
    public static Boolean getBit2(String key, String param) {
        return stringRedisTemplate.opsForValue().getBit(key, hash(param));
    }


    /**
     * 將指定offset偏移量的值設置為1;
     *
     * @param key    bitmap結構的key
     * @param offset 指定的偏移量。
     * @param value  true:即該位設置為1,否則設置為0
     * @return 返回設置該value之前的值。
     */
    public static Boolean setBit(String key, Long offset, boolean value) {
        return stringRedisTemplate.opsForValue().setBit(key, offset, value);
    }

    /**
     * 將指定offset偏移量的值設置為0;
     *
     * @param key    bitmap結構的key
     * @param offset 指定的偏移量。
     * @return 若偏移位上的值為1,那么返回true。
     */
    public static Boolean getBit(String key, long offset) {
        return stringRedisTemplate.opsForValue().getBit(key, offset);
    }

    /**
     * 統計對應的bitmap上value為1的數量
     *
     * @param key bitmap的key
     * @return value等於1的數量
     */
    public static Long bitCount(String key) {
        return stringRedisTemplate.execute((RedisCallback<Long>) con -> con.bitCount(key.getBytes()));
    }

    /**
     * 統計指定范圍中value為1的數量
     *
     * @param key   bitMap中的key
     * @param start 該參數的單位是byte(1byte=8bit),{@code setBit(key,7,true);}進行存儲時,單位是bit。那么只需要統計[0,1]便可以統計到上述set的值。
     * @param end   該參數的單位是byte。
     * @return 在指定范圍[start*8,end*8]內所有value=1的數量
     */
    public static Long bitCount(String key, int start, int end) {
        return stringRedisTemplate.execute((RedisCallback<Long>) con -> con.bitCount(key.getBytes(), start, end));
    }

}

  bean注入

package com.showy.bbs.custom;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtils.applicationContext == null) {
            SpringUtils.applicationContext = applicationContext;
        }
    }
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> c) {
        return getApplicationContext().getBean(c);
    }

    public static <T> T getBean(String name, Class<T> c) {
        return getApplicationContext().getBean(name, c);
    }
}

https://www.jianshu.com/p/305e65de1b13?from=singlemessage


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM