当前位置: 首页 > news >正文

网站建设怎么加音乐企业如何做网络推广

网站建设怎么加音乐,企业如何做网络推广,南京老牌网站建设公司,tp5网站开发步骤大家好,我是雄雄,欢迎关注微信公众号:** 雄雄的小课堂 ** 现在是:2023年2月28日11:01:56 前言 redis大家应该都不陌生,我们在好多场景下都会使用,最近在面试别人的时候,也会问一些关于redis的…

大家好,我是雄雄,欢迎关注微信公众号:** 雄雄的小课堂 **

现在是:2023年2月28日11:01:56

前言

redis大家应该都不陌生,我们在好多场景下都会使用,最近在面试别人的时候,也会问一些关于redis的问题,不多大家说的使用场景,基本都是在登录的时候,将用户信息放在缓存中,别的用的很少。表示一般在开发的时候,也不会进行二次封装redis的类,而是框架中本来就已经整合起来了,所以拿着用就行。

最近我在写一套开源的与社交软件交互的系统,后端采用的是springboot项目,有个地方需要用到缓存redis,所以就将原来的redis封装了下,现在分享出来。

springboot中集成redis

  1. 首先我们需要在pom文件中引入依赖,代码如下:
 <!-- 集成redis依赖  --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
  1. 在需要使用redis的类中,加入自动注入redis的代码:
  @Autowiredprivate RedisTemplate redisTemplate;
  1. 然后接下来我们就可以使用redis了,比如我们想给缓存中放存一个对象,那么我们可以这样写:
  redisTemplate.opsForValue().set("name", "穆雄雄");
  1. 此时我们已经将name放在缓存中,那我们在使用的时候如何从缓存中获取呢?可以如下:
  return key == null ? null : redisTemplate.opsForValue().get(key);
  1. 包括批量添加key:
    Map<String, String> keyAndValue = new HashMap<String.String>();keyAndValue.put("name","穆雄雄");keyAndValue.put("age","20");redisTemplate.opsForValue().multiSet(keyAndValue);

如果在我们项目中使用redis的话,这样操作不仅繁琐,而且写起来也复杂,我们如何让在别的地儿使用的时候更加方便顺手呢,这个时候我们就需要做个简单的封装,

封装redis工具类

下面是我做了个简单的封装,部分也是参考的别人的:

  1. 给一个指定的 key 值附加过期时间
   public boolean expire(String key, long time) {return redisTemplate.expire(key, time, TimeUnit.SECONDS);}
  1. 根据key 获取过期时间
 public long getTime(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}
  1. 移除指定key 的过期时间
  public boolean persist(String key) {return redisTemplate.boundValueOps(key).persist();}
  1. 根据key获取值
    public Object get(String key) {return key == null ? null : redisTemplate.opsForValue().get(key);}
  1. 将值放入缓存
  public void set(String key, String value) {redisTemplate.opsForValue().set(key, value);}
  1. 移除指定key 的过期时间
  public boolean persist(String key) {return redisTemplate.boundValueOps(key).persist();}
  1. 将值放入缓存并设置时间
  public void set(String key, String value, long time) {if (time > 0) {redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);} else {redisTemplate.opsForValue().set(key, value);}}
  1. 批量添加 key (重复的键会覆盖)
  public void batchSet(Map<String, String> keyAndValue) {redisTemplate.opsForValue().multiSet(keyAndValue);}
  1. 批量添加 key-value 只有在键不存在时,才添加,map 中只要有一个key存在,则全部不添加
  public void batchSetIfAbsent(Map<String, String> keyAndValue) {redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);}
  1. 对一个 key-value 的值进行加减操作,如果该 key 不存在 将创建一个key 并赋值该 number,如果 key 存在,但 value 不是长整型 ,将报错
  public Long increment(String key, long number) {return redisTemplate.opsForValue().increment(key, number);}
  1. 对一个 key-value 的值进行加减操作,如果该 key 不存在 将创建一个key 并赋值该 number,如果 key 存在,但 value 不是 纯数字 ,将报错
  public Double increment(String key, double number) {return redisTemplate.opsForValue().increment(key, number);}
  1. 将数据放入set缓存
   public void sSet(String key, String value) {redisTemplate.opsForSet().add(key, value);}
  1. 获取变量中的值
 public Set<Object> members(String key) {return redisTemplate.opsForSet().members(key);}
  1. 随机获取变量中指定个数的元素
 public void randomMembers(String key, long count) {redisTemplate.opsForSet().randomMembers(key, count);}
  1. 随机获取变量中的元素
  public Object randomMember(String key) {return redisTemplate.opsForSet().randomMember(key);}
  1. 弹出变量中的元素
 public Object pop(String key) {return redisTemplate.opsForSet().pop("setValue");}
  1. 移除指定key 的过期时间
  public boolean persist(String key) {return redisTemplate.boundValueOps(key).persist();}
  1. 获取变量中值的长度
  public long size(String key) {return redisTemplate.opsForSet().size(key);}
  1. 根据value从一个set中查询,是否存在
 public boolean sHasKey(String key, Object value) {return redisTemplate.opsForSet().isMember(key, value);}

完整的工具类RedisUtil

package com.hookapi.common;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;/*** @author: muxiongxiong* @date: 2023年02月22日 下午 5:48* 公众号:雄雄的小课堂* 博客:https://blog.csdn.net/qq_34137397* 个人站:http://www.穆雄雄.com* 个人站:http://www.muxiongxiong.cn* @Description: 类的描述*/
@Component
public class RedisUtil {@Autowiredprivate RedisTemplate redisTemplate;/*** 给一个指定的 key 值附加过期时间** @param key* @param time* @return*/public boolean expire(String key, long time) {return redisTemplate.expire(key, time, TimeUnit.SECONDS);}/*** 根据key 获取过期时间** @param key* @return*/public long getTime(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}/*** 根据key 获取过期时间** @param key* @return*/public boolean hasKey(String key) {return redisTemplate.hasKey(key);}/*** 移除指定key 的过期时间** @param key* @return*/public boolean persist(String key) {return redisTemplate.boundValueOps(key).persist();}//- - - - - - - - - - - - - - - - - - - - -  String类型 - - - - - - - - - - - - - - - - - - - -/*** 根据key获取值** @param key 键* @return 值*/public Object get(String key) {return key == null ? null : redisTemplate.opsForValue().get(key);}/*** 将值放入缓存** @param key   键* @param value 值* @return true成功 false 失败*/public void set(String key, String value) {redisTemplate.opsForValue().set(key, value);}/*** 将值放入缓存并设置时间** @param key   键* @param value 值* @param time  时间(秒) -1为无期限* @return true成功 false 失败*/public void set(String key, String value, long time) {if (time > 0) {redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);} else {redisTemplate.opsForValue().set(key, value);}}/*** 批量添加 key (重复的键会覆盖)** @param keyAndValue*/public void batchSet(Map<String, String> keyAndValue) {redisTemplate.opsForValue().multiSet(keyAndValue);}/*** 批量添加 key-value 只有在键不存在时,才添加* map 中只要有一个key存在,则全部不添加** @param keyAndValue*/public void batchSetIfAbsent(Map<String, String> keyAndValue) {redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);}/*** 对一个 key-value 的值进行加减操作,* 如果该 key 不存在 将创建一个key 并赋值该 number* 如果 key 存在,但 value 不是长整型 ,将报错** @param key* @param number*/public Long increment(String key, long number) {return redisTemplate.opsForValue().increment(key, number);}/*** 对一个 key-value 的值进行加减操作,* 如果该 key 不存在 将创建一个key 并赋值该 number* 如果 key 存在,但 value 不是 纯数字 ,将报错** @param key* @param number*/public Double increment(String key, double number) {return redisTemplate.opsForValue().increment(key, number);}//- - - - - - - - - - - - - - - - - - - - -  set类型 - - - - - - - - - - - - - - - - - - - -/*** 将数据放入set缓存** @param key 键* @return*/public void sSet(String key, String value) {redisTemplate.opsForSet().add(key, value);}/*** 获取变量中的值** @param key 键* @return*/public Set<Object> members(String key) {return redisTemplate.opsForSet().members(key);}/*** 随机获取变量中指定个数的元素** @param key   键* @param count 值* @return*/public void randomMembers(String key, long count) {redisTemplate.opsForSet().randomMembers(key, count);}/*** 随机获取变量中的元素** @param key 键* @return*/public Object randomMember(String key) {return redisTemplate.opsForSet().randomMember(key);}/*** 弹出变量中的元素** @param key 键* @return*/public Object pop(String key) {return redisTemplate.opsForSet().pop("setValue");}/*** 获取变量中值的长度** @param key 键* @return*/public long size(String key) {return redisTemplate.opsForSet().size(key);}/*** 根据value从一个set中查询,是否存在** @param key   键* @param value 值* @return true 存在 false不存在*/public boolean sHasKey(String key, Object value) {return redisTemplate.opsForSet().isMember(key, value);}/*** 检查给定的元素是否在变量中。** @param key 键* @param obj 元素对象* @return*/public boolean isMember(String key, Object obj) {return redisTemplate.opsForSet().isMember(key, obj);}/*** 转移变量的元素值到目的变量。** @param key     键* @param value   元素对象* @param destKey 元素对象* @return*/public boolean move(String key, String value, String destKey) {return redisTemplate.opsForSet().move(key, value, destKey);}/*** 批量移除set缓存中元素** @param key    键* @param values 值* @return*/public void remove(String key, Object... values) {redisTemplate.opsForSet().remove(key, values);}/*** 通过给定的key求2个set变量的差值** @param key     键* @param destKey 键* @return*/public Set<Set> difference(String key, String destKey) {return redisTemplate.opsForSet().difference(key, destKey);}//- - - - - - - - - - - - - - - - - - - - -  hash类型 - - - - - - - - - - - - - - - - - - - -/*** 加入缓存** @param key 键* @param map 键* @return*/public void add(String key, Map<String, String> map) {redisTemplate.opsForHash().putAll(key, map);}/*** 获取 key 下的 所有  hashkey 和 value** @param key 键* @return*/public Map<Object, Object> getHashEntries(String key) {return redisTemplate.opsForHash().entries(key);}/*** 验证指定 key 下 有没有指定的 hashkey** @param key* @param hashKey* @return*/public boolean hashKey(String key, String hashKey) {return redisTemplate.opsForHash().hasKey(key, hashKey);}/*** 获取指定key的值string** @param key  键* @param key2 键* @return*/public String getMapString(String key, String key2) {return redisTemplate.opsForHash().get("map1", "key1").toString();}/*** 获取指定的值Int** @param key  键* @param key2 键* @return*/public Integer getMapInt(String key, String key2) {return (Integer) redisTemplate.opsForHash().get("map1", "key1");}/*** 弹出元素并删除** @param key 键* @return*/public String popValue(String key) {return redisTemplate.opsForSet().pop(key).toString();}/*** 删除指定 hash 的 HashKey** @param key* @param hashKeys* @return 删除成功的 数量*/public Long delete(String key, String... hashKeys) {return redisTemplate.opsForHash().delete(key, hashKeys);}/*** 给指定 hash 的 hashkey 做增减操作** @param key* @param hashKey* @param number* @return*/public Long increment(String key, String hashKey, long number) {return redisTemplate.opsForHash().increment(key, hashKey, number);}/*** 给指定 hash 的 hashkey 做增减操作** @param key* @param hashKey* @param number* @return*/public Double increment(String key, String hashKey, Double number) {return redisTemplate.opsForHash().increment(key, hashKey, number);}/*** 获取 key 下的 所有 hashkey 字段** @param key* @return*/public Set<Object> hashKeys(String key) {return redisTemplate.opsForHash().keys(key);}/*** 获取指定 hash 下面的 键值对 数量** @param key* @return*/public Long hashSize(String key) {return redisTemplate.opsForHash().size(key);}//- - - - - - - - - - - - - - - - - - - - -  list类型 - - - - - - - - - - - - - - - - - - - -/*** 在变量左边添加元素值** @param key* @param value* @return*/public void leftPush(String key, Object value) {redisTemplate.opsForList().leftPush(key, value);}/*** 获取集合指定位置的值。** @param key* @param index* @return*/public Object index(String key, long index) {return redisTemplate.opsForList().index("list", 1);}/*** 获取指定区间的值。** @param key* @param start* @param end* @return*/public List<Object> range(String key, long start, long end) {return redisTemplate.opsForList().range(key, start, end);}/*** 把最后一个参数值放到指定集合的第一个出现中间参数的前面,* 如果中间参数值存在的话。** @param key* @param pivot* @param value* @return*/public void leftPush(String key, String pivot, String value) {redisTemplate.opsForList().leftPush(key, pivot, value);}/*** 向左边批量添加参数元素。** @param key* @param values* @return*/public void leftPushAll(String key, String... values) {
//        redisTemplate.opsForList().leftPushAll(key,"w","x","y");redisTemplate.opsForList().leftPushAll(key, values);}/*** 向集合最右边添加元素。** @param key* @param value* @return*/public void leftPushAll(String key, String value) {redisTemplate.opsForList().rightPush(key, value);}/*** 向左边批量添加参数元素。** @param key* @param values* @return*/public void rightPushAll(String key, String... values) {//redisTemplate.opsForList().leftPushAll(key,"w","x","y");redisTemplate.opsForList().rightPushAll(key, values);}/*** 向已存在的集合中添加元素。** @param key* @param value* @return*/public void rightPushIfPresent(String key, Object value) {redisTemplate.opsForList().rightPushIfPresent(key, value);}/*** 向已存在的集合中添加元素。** @param key* @return*/public long listLength(String key) {return redisTemplate.opsForList().size(key);}/*** 移除集合中的左边第一个元素。** @param key* @return*/public void leftPop(String key) {redisTemplate.opsForList().leftPop(key);}/*** 移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。** @param key* @return*/public void leftPop(String key, long timeout, TimeUnit unit) {redisTemplate.opsForList().leftPop(key, timeout, unit);}/*** 移除集合中右边的元素。** @param key* @return*/public void rightPop(String key) {redisTemplate.opsForList().rightPop(key);}/*** 移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。** @param key* @return*/public void rightPop(String key, long timeout, TimeUnit unit) {redisTemplate.opsForList().rightPop(key, timeout, unit);}
}

然后我们可以看看如何在控制器中调用:

同样还是需要先注入:

 @Autowiredprivate RedisUtil redisUtil;

然后就可以使用了,比如我们给缓存中存放一个值:

 redisUtil.set(user,topicName);

至此,就完成啦。


文章转载自:
http://dinncodiplomapiece.ssfq.cn
http://dinncocryptonym.ssfq.cn
http://dinncohoming.ssfq.cn
http://dinncoconsensus.ssfq.cn
http://dinncoavulse.ssfq.cn
http://dinncoventose.ssfq.cn
http://dinnconaboth.ssfq.cn
http://dinncoelaeometer.ssfq.cn
http://dinncoaaal.ssfq.cn
http://dinncokempis.ssfq.cn
http://dinncosauch.ssfq.cn
http://dinncokanchenjunga.ssfq.cn
http://dinncocontrastimulant.ssfq.cn
http://dinncoberceuse.ssfq.cn
http://dinncohackie.ssfq.cn
http://dinncoireland.ssfq.cn
http://dinncomalvinas.ssfq.cn
http://dinncoikon.ssfq.cn
http://dinncopeptid.ssfq.cn
http://dinncoteddy.ssfq.cn
http://dinncopolite.ssfq.cn
http://dinncowoodiness.ssfq.cn
http://dinncoincunabulum.ssfq.cn
http://dinncoussr.ssfq.cn
http://dinncorubigo.ssfq.cn
http://dinnconationalization.ssfq.cn
http://dinncohakodate.ssfq.cn
http://dinncoligularia.ssfq.cn
http://dinncoidealize.ssfq.cn
http://dinncoenigma.ssfq.cn
http://dinncocriminous.ssfq.cn
http://dinncointerlanguage.ssfq.cn
http://dinncosynchronise.ssfq.cn
http://dinncosemileptonic.ssfq.cn
http://dinncoawning.ssfq.cn
http://dinncocapitular.ssfq.cn
http://dinncoscheduler.ssfq.cn
http://dinncodeus.ssfq.cn
http://dinncoalas.ssfq.cn
http://dinncoescot.ssfq.cn
http://dinncotrichloroethylene.ssfq.cn
http://dinncogrounded.ssfq.cn
http://dinncomucoserous.ssfq.cn
http://dinncopleuroperitoneal.ssfq.cn
http://dinncochitling.ssfq.cn
http://dinncolambie.ssfq.cn
http://dinncoisolog.ssfq.cn
http://dinnconookie.ssfq.cn
http://dinncoquadriphony.ssfq.cn
http://dinncolossy.ssfq.cn
http://dinncoensanguine.ssfq.cn
http://dinncotoolholder.ssfq.cn
http://dinncoexuviation.ssfq.cn
http://dinncoroughcast.ssfq.cn
http://dinncointernalize.ssfq.cn
http://dinncocystoid.ssfq.cn
http://dinncocmitosis.ssfq.cn
http://dinncoparadisaical.ssfq.cn
http://dinncopharmaceutics.ssfq.cn
http://dinncofishbed.ssfq.cn
http://dinncodammar.ssfq.cn
http://dinncoroughscuff.ssfq.cn
http://dinncosubtend.ssfq.cn
http://dinncosaurel.ssfq.cn
http://dinncoayc.ssfq.cn
http://dinncotheorist.ssfq.cn
http://dinncomolly.ssfq.cn
http://dinncosnagged.ssfq.cn
http://dinncodarwinism.ssfq.cn
http://dinncooverbite.ssfq.cn
http://dinncochuffy.ssfq.cn
http://dinncogritty.ssfq.cn
http://dinncowhitefish.ssfq.cn
http://dinncoafterheat.ssfq.cn
http://dinncopostnasal.ssfq.cn
http://dinncowidf.ssfq.cn
http://dinncoretrogradation.ssfq.cn
http://dinncoflattish.ssfq.cn
http://dinncoeavesdrop.ssfq.cn
http://dinncominikin.ssfq.cn
http://dinncomobility.ssfq.cn
http://dinncoparies.ssfq.cn
http://dinncoagonoze.ssfq.cn
http://dinncosankhya.ssfq.cn
http://dinncogemara.ssfq.cn
http://dinncoswivelpin.ssfq.cn
http://dinncothat.ssfq.cn
http://dinncosedition.ssfq.cn
http://dinncomorosely.ssfq.cn
http://dinncotinnery.ssfq.cn
http://dinncounbailable.ssfq.cn
http://dinncorubied.ssfq.cn
http://dinncoolive.ssfq.cn
http://dinncocorpuscular.ssfq.cn
http://dinncohorsenapping.ssfq.cn
http://dinnconormality.ssfq.cn
http://dinncohibernate.ssfq.cn
http://dinncopilosity.ssfq.cn
http://dinncofavous.ssfq.cn
http://dinncodefluent.ssfq.cn
http://www.dinnco.com/news/160095.html

相关文章:

  • 织梦网站怎样做安全防护店铺引流的30种方法
  • 蓝色网站设计中国新闻网发稿
  • 做网页局域网站点配置网络营销服务工具
  • 省建设执业资格注册中心网站最新seo黑帽技术工具软件
  • 贵阳网站开发培训推广文案
  • wordpress 文章长 隐藏seo如何优化关键词排名
  • wordpress手机版侧栏导航条淘宝seo是什么
  • 专业手机网站建设哪家好微信营销方式有哪些
  • wordpress配置主题seo的研究对象
  • 青岛知名网站建设哪家好百度推广客户端下载网址
  • 做电影网站侵权吗网站优化推广seo
  • 网站建设 昆明 价格推广类软文案例
  • 公司常用网站开发软件万能搜索引擎
  • 土木建筑网站国内免费域名注册
  • 做网站需要的设备哪里可以接广告
  • 博山区住房和城乡建设局网站百度搜索引擎的原理
  • 重庆网站建设厦门百度公司
  • 安徽建站费用开发app需要多少资金
  • 济南集团网站建设自媒体发稿
  • 武汉做网站费用河南专业网站建设
  • 网站双收录怎么做301跳转百度站长收录入口
  • 领导交给你一个网站你该怎么做网站优化系统
  • 做外贸翻译用哪个网站好搜索引擎seo推广
  • 用discuz做商城网站龙华网站建设
  • 下载资料免费网站最新网络营销方式有哪些
  • 云南工贸网站建设线下营销方式主要有哪些
  • 外包公司和劳务派遣哪个好一点seo日常工作都做什么的
  • 营销型网站建设概述点击精灵seo
  • 北京市建设工程审核网站网站排名提升软件
  • 电商网站如何做引流社区营销