Login
网站首页 > 文章中心 > 其它

Redis除了做缓存|*|#8211;Redis做消息队列/Redis做分布式锁/Redis做接口限流_redis做登录缓存

作者:小编 更新时间:2023-08-10 13:09:13 浏览量:70人看过

①.、用Redis实现消息队列

Redis除了做缓存|*|#8211;Redis做消息队列/Redis做分布式锁/Redis做接口限流_redis做登录缓存

用命令lpush入队,rpop出队

用rpop会存在一个问题,及需要不停调用rpop方法查看List中是否有未处理的消息,每次调用都会发起一次连接,

这样会造成不必要的浪费.所以可以使用brpop指令,这个指令只有在有元素时才返回,没有则会阻塞直到超时

返回null,于是代码改为:

brpop支持多个列表(队列)

brpop指令是支持队列优先级的,比如这个例子中MESSAGE_KEY的优先级大于testKey(顺序决定).

如果两个列表中都有元素,会优先返回优先级高的列表中的元素,所以这儿优先返回MESSAGE_KEY

0表示不限制等待,会一直阻塞在这儿

当定时服务进行集群部署时,如何保证只有一个定时任务执行,其他任务不执行?

Redis除了做缓存|*|#8211;Redis做消息队列/Redis做分布式锁/Redis做接口限流_redis做登录缓存

某接口/test,在10s内只能被调用1000次,限流逻辑如下:

我们用接口路径作为key,调用次数作为value

首先获取接口调用次数


判断如果为空则设置该接口被调用一次


用setex命令可以保证原子性

判断如果不为空则该接口调用次数加1


下面是操作redis的工具

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.SortingParams;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
@Slf4j
public class RedisUtil{

private JedisPool jedisPool;

/**
 * 

*

*

*

*
@param key * @param indexdb 选择redis库 0-15 * @return 成功返回value 失败返回null */ public String get(String key,int indexdb) { Jedis jedis = null; String value = null; try { jedis = jedisPool.getResource(); value = jedis.get(key); } catch (Exception e) { } finally { return value; /** *

*

*

*

*
@param key * @param indexdb 选择redis库 0-15 * @return 成功返回value 失败返回null */ public byte[] get(byte[] key,int indexdb) { Jedis jedis = null; byte[] value = null; try { jedis = jedisPool.getResource(); value = jedis.get(key); } catch (Exception e) { } finally { return value; /** *

*

*

*

*
@param key * @param value * @param indexdb 选择redis库 0-15 * @return 成功 返回OK 失败返回 0 */ public String set(String key, String value,int indexdb) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.set(key, value); } catch (Exception e) { return "0"; } finally { /** *

*

*

*

*
@param key * @param value * @param indexdb 选择redis库 0-15 * @return 成功 返回OK 失败返回 0 */ public String set(byte[] key, byte[] value,int indexdb) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.set(key, value); } catch (Exception e) { return "0"; } finally { /** *

*

*
@param keys 一个key 也可以使 string 数组 * @return 返回删除成功的个数 */ public Long del(String... keys) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.del(keys); } catch (Exception e) { return 0L; } finally { /** *

*

*
@param indexdb 选择redis库 0-15 * @param keys 一个key 也可以使 string 数组 * @return 返回删除成功的个数 */ public Long del(int indexdb,String... keys) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.del(keys); } catch (Exception e) { return 0L; } finally { /** *

*

*
@param indexdb 选择redis库 0-15 * @param keys 一个key 也可以使 string 数组 * @return 返回删除成功的个数 */ public Long del(int indexdb,byte[]... keys) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.del(keys); } catch (Exception e) { return 0L; } finally { /** *

*

*
@param key * @param str * @return 成功返回 添加后value的长度 失败 返回 添加的 value 的长度 异常返回0L */ public Long append(String key, String str) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.append(key, str); } catch (Exception e) { return 0L; } finally { return res; /** *

*

*
@param key * @return true OR false */ public Boolean exists(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.exists(key); } catch (Exception e) { return false; } finally { /** *

*

*
@return 总是返回 OK */ public String flushDB() { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.flushDB(); } catch (Exception e) { } finally { return null; /** *

*

*
@param key * @param value * @return 成功返回1 如果存在 和 发生异常 返回 0 */ public Long expire(String key, int value, int indexdb) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.expire(key, value); } catch (Exception e) { return 0L; } finally { /** *

*

*
@param key * @return 当 key 不存在时,返回 -2 .当 key 存在但没有设置剩余生存时间时,返回 -1 .否则,以秒为单位,返回 key */ public Long ttl(String key,int indexdb) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.ttl(key); } catch (Exception e) { return 0L; } finally { /** *

*

*
@param key * @return 当生存时间移除成功时,返回 1 .如果 key 不存在或 key 没有设置生存时间,返回 0 , 发生异常 返回 -1 */ public Long persist(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.persist(key); } catch (Exception e) { return -1L; } finally { /** *

*

*
@param key * @param seconds * @param value * @return 设置成功时返回 OK .当 seconds 参数不合法时,返回一个错误. */ public String setex(String key, int seconds, String value,int indexdb) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.setex(key, seconds, value); } catch (Exception e) { } finally { return null; /** *

*

*
@param key * @param value * @return 成功返回1 如果存在 和 发生异常 返回 0 */ public Long setnx(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.setnx(key, value); } catch (Exception e) { return 0L; } finally { /** *

*

*

*

*
@param key * @param value * @return 返回给定 key 的旧值.当 key 没有旧值时,也即是, key 不存在时,返回 nil */ public String getSet(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.getSet(key, value); } catch (Exception e) { } finally { return null; /** *

*

*
@param key * @param value * @param seconds * @return 成功返回OK 失败和异常返回null */ public String setex(String key, String value, int seconds,int indexdb) { Jedis jedis = null; String res = null; try { jedis = jedisPool.getResource(); // jedis.select(indexdb); res = jedis.setex(key, seconds, value); } catch (Exception e) { } finally { return res; /** *

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*
@param key * @param str * @param offset * @return 返回替换后 value 的长度 */ public Long setrange(String key, String str, int offset) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.setrange(key, offset, str); } catch (Exception e) { return 0L; } finally { /** *

*

*
@param keys * @return 成功返回value的集合, 失败返回null的集合 ,异常返回空 */ public List mget(String... keys) { Jedis jedis = null; List values = null; try { jedis = jedisPool.getResource(); values = jedis.mget(keys); } catch (Exception e) { } finally { return values; /** *

*

*

*

*

*

*
@param keysvalues * @return 成功返回OK 失败 异常 返回 null */ public String mset(String... keysvalues) { Jedis jedis = null; String res = null; try { jedis = jedisPool.getResource(); res = jedis.mset(keysvalues); } catch (Exception e) { } finally { return res; /** *

*

*

*

*

*

*
@param keysvalues * @return 成功返回1 失败返回0 */ public Long msetnx(String... keysvalues) { Jedis jedis = null; Long res = 0L; try { jedis = jedisPool.getResource(); res = jedis.msetnx(keysvalues); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param value * @return 旧值 如果key不存在 则返回null */ public String getset(String key, String value) { Jedis jedis = null; String res = null; try { jedis = jedisPool.getResource(); res = jedis.getSet(key, value); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param startOffset * @param endOffset * @return 如果没有返回null */ public String getrange(String key, int startOffset, int endOffset) { Jedis jedis = null; String res = null; try { jedis = jedisPool.getResource(); res = jedis.getrange(key, startOffset, endOffset); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @return 加值后的结果 */ public Long incr(String key,int indexdb) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.incr(key); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param integer * @return */ public Long incrBy(String key, Long integer) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.incrBy(key, integer); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @return */ public Long decr(String key,int indexdb) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.decr(key); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param integer * @return */ public Long decrBy(String key, Long integer) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.decrBy(key, integer); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @return 失败返回null */ public Long serlen(String key) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.strlen(key); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param field * @param value * @return 如果存在返回0 异常返回null */ public Long hset(String key, String field, String value) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.hset(key, field, value); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param field * @param value * @return */ public Long hsetnx(String key, String field, String value) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.hsetnx(key, field, value); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param hash * @return 返回OK 异常返回null */ public String hmset(String key, Map hash, int indexdb) { Jedis jedis = null; String res = null; try { jedis = jedisPool.getResource(); res = jedis.hmset(key, hash); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param field * @return 没有返回null */ public String hget(String key, String field) { Jedis jedis = null; String res = null; try { jedis = jedisPool.getResource(); res = jedis.hget(key, field); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param fields * @return */ public List hmget(String key, int indexdb, String... fields) { Jedis jedis = null; List res = null; try { jedis = jedisPool.getResource(); res = jedis.hmget(key, fields); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param field * @param value * @return */ public Long hincrby(String key, String field, Long value) { Jedis jedis = null; Long res = null; try { jedis = jedisPool.getResource(); res = jedis.hincrBy(key, field, value); } catch (Exception e) { } finally { return res; /** *

*

*
@param key * @param field * @return */ public Boolean hexists(String key, String field) { Jedis jedis = null; Boolean res = false; try { jedis = jedisPool.getResource(); res = jedis.hexists(key, field); } catch (Exception e) { } finally { return res; /** *

以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章