欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot基础之RedisUtils工具类

程序员文章站 2022-03-07 12:50:54
springboot整合redis引入redis依赖 org.springframew...

springboot整合redis

引入redis依赖

 <!-- redis-->
<dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>

设置redis的template

Springboot基础之RedisUtils工具类

redisconfig.java

package cn.wideth.config;

import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.data.redis.connection.redisconnectionfactory;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.serializer.redisserializer;

/**
 * 编写配置类,构造redistemplate
 * 这个springboot已经帮我们配了,
 * 但是默认object,我想改成string
 */
@configuration
public class redisconfig {

    @bean
    public redistemplate<string, object> redistemplate(redisconnectionfactory factory) {

        redistemplate<string, object> template = new redistemplate<>();
        template.setconnectionfactory(factory);
        // 设置key的序列化方式
        template.setkeyserializer(redisserializer.string());
        // 设置value的序列化方式
        template.setvalueserializer(redisserializer.json());
        // 设置hash的key的序列化方式
        template.sethashkeyserializer(redisserializer.string());
        // 设置hash的value的序列化方式
        template.sethashvalueserializer(redisserializer.json());
        template.afterpropertiesset();
        return template;
    }

}

设置redis连接信息

Springboot基础之RedisUtils工具类

redis操作5种常见的数据类型

redis工具类

redistemplate api

1.opsforvalue -> string

2.opsforset -> set

3.opsforhash -> hash

4.opsforzset -> sortset

5.opsforlist -> list队列

代码

package cn.wideth.util.other;


import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.*;
import java.util.list;
import java.util.set;
import java.util.concurrent.timeunit;

public class redisutils {

    @autowired
    private redistemplate<string, object> redistemplate;

    private static double size = math.pow(2, 32);


    /**
     * 写入缓存
     * @param key
     * @param offset 位 8bit=1byte
     * @return
     */
    public boolean setbit(string key, long offset, boolean isshow) {
        boolean result = false;
        try {
            valueoperations<string, object> operations = redistemplate.opsforvalue();
            operations.setbit(key, offset, isshow);
            result = true;
        } catch (exception e) {
            e.printstacktrace();
        }
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param offset
     * @return
     */
    public boolean getbit(string key, long offset) {
        boolean result = false;
        try {
            valueoperations<string, object> operations = redistemplate.opsforvalue();
            result = operations.getbit(key, offset);
        } catch (exception e) {
            e.printstacktrace();
        }
        return result;
    }


    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final string key, object value) {
        boolean result = false;
        try {
            valueoperations<string, object> operations = redistemplate.opsforvalue();
            operations.set(key, value);
            result = true;
        } catch (exception e) {
            e.printstacktrace();
        }
        return result;
    }
    

    /**
     * 写入缓存设置时效时间
     * @param key
     * @param value
     * @return
     */
    public boolean set(final string key, object value, long expiretime) {
        boolean result = false;
        try {
            valueoperations<string, object> operations = redistemplate.opsforvalue();
            operations.set(key, value);
            redistemplate.expire(key, expiretime, timeunit.seconds);
            result = true;
        } catch (exception e) {
            e.printstacktrace();
        }
        return result;
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final string... keys) {
        for (string key : keys) {
            remove(key);
        }
    }


    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final string key) {
        if (exists(key)) {
            redistemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final string key) {
        return redistemplate.haskey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public object get(final string key) {
        object result = null;
        valueoperations<string, object> operations = redistemplate.opsforvalue();
        result = operations.get(key);
        return result;
    }

    /**
     * 哈希 添加
     *
     * @param key
     * @param hashkey
     * @param value
     */
    public void hmset(string key, object hashkey, object value) {
        hashoperations<string, object, object> hash = redistemplate.opsforhash();
        hash.put(key, hashkey, value);
    }

    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashkey
     * @return
     */
    public object hmget(string key, object hashkey) {
        hashoperations<string, object, object> hash = redistemplate.opsforhash();
        return hash.get(key, hashkey);
    }

    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void lpush(string k, object v) {
        listoperations<string, object> list = redistemplate.opsforlist();
        list.rightpush(k, v);
    }

    /**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public list<object> lrange(string k, long l, long l1) {
        listoperations<string, object> list = redistemplate.opsforlist();
        return list.range(k, l, l1);
    }

    /**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void add(string key, object value) {
        setoperations<string, object> set = redistemplate.opsforset();
        set.add(key, value);
    }

    /**
     * 集合获取
     * @param key
     * @return
     */
    public set<object> setmembers(string key) {
        setoperations<string, object> set = redistemplate.opsforset();
        return set.members(key);
    }

    /**
     * 有序集合添加
     * @param key
     * @param value
     * @param scoure
     */
    public void zadd(string key, object value, double scoure) {
        zsetoperations<string, object> zset = redistemplate.opsforzset();
        zset.add(key, value, scoure);
    }

    /**
     * 有序集合获取
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */
    public set<object> rangebyscore(string key, double scoure, double scoure1) {
        zsetoperations<string, object> zset = redistemplate.opsforzset();
        redistemplate.opsforvalue();
        return zset.rangebyscore(key, scoure, scoure1);
    }


    //第一次加载的时候将数据加载到redis中
    public void savedatatoredis(string name) {
        double index = math.abs(name.hashcode() % size);
        long indexlong = new double(index).longvalue();
        boolean availableusers = setbit("availableusers", indexlong, true);
    }

    //第一次加载的时候将数据加载到redis中
    public boolean getdatatoredis(string name) {

        double index = math.abs(name.hashcode() % size);
        long indexlong = new double(index).longvalue();
        return getbit("availableusers", indexlong);
    }

    /**
     * 有序集合获取排名
     * @param key 集合名称
     * @param value 值
     */
    public long zrank(string key, object value) {
        zsetoperations<string, object> zset = redistemplate.opsforzset();
        return zset.rank(key,value);
    }


    /**
     * 有序集合获取排名
     * @param key
     */
    public set<zsetoperations.typedtuple<object>> zrankwithscore(string key, long start,long end) {
        zsetoperations<string, object> zset = redistemplate.opsforzset();
        set<zsetoperations.typedtuple<object>> ret = zset.rangewithscores(key,start,end);
        return ret;
    }

    /**
     * 有序集合添加
     * @param key
     * @param value
     */
    public double zsetscore(string key, object value) {
        zsetoperations<string, object> zset = redistemplate.opsforzset();
        return zset.score(key,value);
    }


    /**
     * 有序集合添加分数
     * @param key
     * @param value
     * @param scoure
     */
    public void incrementscore(string key, object value, double scoure) {
        zsetoperations<string, object> zset = redistemplate.opsforzset();
        zset.incrementscore(key, value, scoure);
    }


    /**
     * 有序集合获取排名
     * @param key
     */
    public set<zsetoperations.typedtuple<object>> reversezrankwithscore(string key, long start,long end) {
        zsetoperations<string, object> zset = redistemplate.opsforzset();
        set<zsetoperations.typedtuple<object>> ret = zset.reverserangebyscorewithscores(key,start,end);
        return ret;
    }

    /**
     * 有序集合获取排名
     * @param key
     */
    public set<zsetoperations.typedtuple<object>> reversezrankwithrank(string key, long start, long end) {
        zsetoperations<string, object> zset = redistemplate.opsforzset();
        set<zsetoperations.typedtuple<object>> ret = zset.reverserangewithscores(key, start, end);
        return ret;
    }

    
}

到此这篇关于springboot基础之redisutils工具类的文章就介绍到这了,更多相关redisutils工具类内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!