温州市手机网站制作,c2c电子商务的特点,手机系统优化软件,网站备案查询 站长1、redis数据类型
1.1、5种数据类型
redis存储的是key-value结构的数据#xff0c;其中key是字符串类型#xff0c;value有5种常用的数据类型#xff1a;字符串 string、哈希 hash、列表 list、集合 set、有序集合 sorted set / zset。 字符串(string)#xff1a;普通字符…1、redis数据类型
1.1、5种数据类型
redis存储的是key-value结构的数据其中key是字符串类型value有5种常用的数据类型字符串 string、哈希 hash、列表 list、集合 set、有序集合 sorted set / zset。 字符串(string)普通字符串Redis中最简单的数据类型。哈希(hash)也叫散列类似于Java中的HashMap结构。列表(list)按照插入顺序排序可以有重复元素类似于Java中的LinkedList。集合(set)无序集合没有重复元素类似于Java中的HashSet。有序集合(sorted set/zset)集合中每个元素关联一个分数(score)根据分数升序排序没有重复元素字符串类型操作指令开头一般为s开头SET 、GET、SETEX 、SETNX 哈希操作命令操作指令开头一般为h开头HSET 、HGET 、HDEL 、HKEYS 、HVALS 列表操作命令指令开头一般为l开头LPUSH 、LRANGE 、RPOP 、LLEN 、BRPOP 集合操作命令指令开头一般为s开头SADD 、SMEMBERS 、SCARD 、SINTER 、SUNION 、SREM 有序集合操作命令指令开头一般为z开头ZADD 、ZRANGE 、ZINCRBY 、ZREM 1.2、字符串操作命令
SET key value 设置指定key的值 GET key 获取指定key的值 SETEX key seconds value 设置指定key的值并将 key 的过期时间设为 seconds 秒 100秒之后它会自动销毁
SETNX key value 只有在 key 不存在时设置 key 的值 1.3、哈希操作命令
redis hash 是一个string类型的 field 和 value 的映射表hash特别适合用于存储对象
HSET key field value 将哈希表 key 中的字段 field 的值设为 value HGET key field 获取存储在哈希表中指定字段的值 HDEL key field 删除存储在哈希表中的指定字段 HKEYS key 获取哈希表中所有字段 HVALS key 获取哈希表中所有值 1.4、列表操作命令 redis 列表是简单的字符串列表按照插入顺序排序
LPUSH key value1 [value2] 将一个或多个值插入到列表头部 LRANGE key start stop 获取列表指定范围内的元素 RPOP key 移除并获取列表最后一个元素 1.5、集合操作命令
redis set 是string类型的无序集合。集合成员是唯一的这就意味着集合中不能出现重复的数据
SADD key member1 [member2] 向集合添加一个或多个成员 SMEMBERS key 返回集合中的所有成员 SCARD key 获取集合的成员数 SINTER key1 [key2] 返回给定所有集合的交集 SUNION key1 [key2] 返回所有给定集合的并集 SREM key member1 [member2] 移除集合中一个或多个成员 1.6、有序集合操作命令
redis有序集合是string类型元素的集合且不允许有重复成员。每个元素都会关联一个double类型的分数根据分数来进行排序
ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员 ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合中指定区间内的成员 ZINCRBY key increment member 有序集合中对指定成员的分数加上增量 increment ZREM key member [member ...] 移除有序集合中的一个或多个成员 1.7、通用命令
KEYS pattern 查找所有符合给定模式(pattern)的 key EXISTS key 检查给定 key 是否存在 TYPE key 返回 key 所储存的值的类型 DEL key 该命令用于在 key 存在是删除 key 2、java中使用redis
2.1、引入maven依赖 pom.xml
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency
RedisTemplate对相关api进行了归类封装,将同一类型操作封装为operation接口分类如下
ValueOperations string数据操作SetOperations set类型数据操作ZSetOperations zset类型数据操作HashOperations hash类型的数据操作ListOperations list类型的数据操作 application.yml中
spring:application:name: springdataredis_demo#Redis相关配置redis:host: localhostport: 6379#password: 123456database: 0 #默认操作的是0号数据库jedis:#Redis连接池配置pool:max-active: 8 #最大连接数max-wait: 1ms #连接池最大阻塞等待时间max-idle: 4 #连接池中的最大空闲连接min-idle: 0 #连接池中的最小空闲连接/*** Redis配置类*/Configuration
public class RedisConfig extends CachingConfigurerSupport {Beanpublic RedisTemplateObject, Object redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplateObject, Object redisTemplate new RedisTemplate();//默认的Key序列化器为JdkSerializationRedisSerializer//String类型序列化器redisTemplate.setKeySerializer(new StringRedisSerializer());//hash类型序列化器redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setConnectionFactory(connectionFactory);return redisTemplate;}}注当前配置类不是必须的因为 Spring Boot 框架会自动装配 RedisTemplate 对象但是默认的key序列化器为JdkSerializationRedisSerializer导致我们存到Redis中后的数据和原始数据有差别故设置为StringRedisSerializer序列化器
2.2、操作String类型数据
SpringBootTest
RunWith(SpringRunner.class)
public class SpringDataRedisTest {//yml中配置支持RedisTemplate对象自动装配Autowiredprivate RedisTemplate redisTemplate;/*** 操作String类型数据*/Testpublic void testString(){//ValueOperations valueOperations redisTemplate.opsForValue();redisTemplate.opsForValue().set(city123,beijing);//打印输出String value (String) redisTemplate.opsForValue().get(city123);System.out.println(value);//设置超时时间redisTemplate.opsForValue().set(key1,value1,10l, TimeUnit.SECONDS);//当key不存在才设置valueBoolean aBoolean redisTemplate.opsForValue().setIfAbsent(city1234, nanjing);System.out.println(aBoolean);}
}2.3、操作Hash类型数据
SpringBootTest
RunWith(SpringRunner.class)
public class SpringDataRedisTest {Autowiredprivate RedisTemplate redisTemplate;/*** 操作Hash类型数据*/Testpublic void testHash(){HashOperations hashOperations redisTemplate.opsForHash();//存值hashOperations.put(002,name,xiaoshen);hashOperations.put(002,age,20);hashOperations.put(002,address,nj);//取值String age (String) hashOperations.get(002, age);System.out.println(age);//获得hash结构中的所有字段Set keys hashOperations.keys(002);for (Object key : keys) {System.out.println(key);}//获得hash结构中的所有值List values hashOperations.values(002);for (Object value : values) {System.out.println(value);}}
}2.4、操作List类型数据
SpringBootTest
RunWith(SpringRunner.class)
public class SpringDataRedisTest {Autowiredprivate RedisTemplate redisTemplate;/*** 操作List类型的数据*/Testpublic void testList(){ListOperations listOperations redisTemplate.opsForList();//存1个值listOperations.leftPush(mylist,a);//存多个值listOperations.leftPushAll(mylist,b,c,d);//取值ListString mylist listOperations.range(mylist, 0, -1);for (String value : mylist) {System.out.println(value);}//获得列表长度 llenLong size listOperations.size(mylist);int lSize size.intValue();for (int i 0; i lSize; i) {//出队列String element (String) listOperations.rightPop(mylist);System.out.println(element);}}
}2.5、操作Set类型数据
SpringBootTest
RunWith(SpringRunner.class)
public class SpringDataRedisTest {Autowiredprivate RedisTemplate redisTemplate;/*** 操作Set类型的数据*/Testpublic void testSet(){SetOperations setOperations redisTemplate.opsForSet();//存值setOperations.add(myset,a,b,c,a);//取值SetString myset setOperations.members(myset);for (String o : myset) {System.out.println(o);}//删除成员setOperations.remove(myset,a,b);//取值myset setOperations.members(myset);for (String o : myset) {System.out.println(o);}}
}2.6、操作ZSet类型数据
SpringBootTest
RunWith(SpringRunner.class)
public class SpringDataRedisTest {Autowiredprivate RedisTemplate redisTemplate;/*** 操作ZSet类型的数据*/Testpublic void testZset(){ZSetOperations zSetOperations redisTemplate.opsForZSet();//存值zSetOperations.add(myZset,a,10.0);zSetOperations.add(myZset,b,11.0);zSetOperations.add(myZset,c,12.0);zSetOperations.add(myZset,a,13.0);//取值SetString myZset zSetOperations.range(myZset, 0, -1);for (String s : myZset) {System.out.println(s);}//修改分数zSetOperations.incrementScore(myZset,b,20.0);//取值myZset zSetOperations.range(myZset, 0, -1);for (String s : myZset) {System.out.println(s);}//删除成员zSetOperations.remove(myZset,a,b);//取值myZset zSetOperations.range(myZset, 0, -1);for (String s : myZset) {System.out.println(s);}}
}2.7、redis 通用操作
SpringBootTest
RunWith(SpringRunner.class)
public class SpringDataRedisTest {Autowiredprivate RedisTemplate redisTemplate;/*** 通用操作针对不同的数据类型都可以操作*/Testpublic void testCommon(){//获取Redis中所有的keySetString keys redisTemplate.keys(*);for (String key : keys) {System.out.println(key);}//判断某个key是否存在Boolean itcast redisTemplate.hasKey(dongli);System.out.println(itcast);//删除指定keyredisTemplate.delete(myZset);//获取指定key对应的value的数据类型DataType dataType redisTemplate.type(myset);System.out.println(dataType.name());}
} 文章转载自: http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.wqngt.cn.gov.cn.wqngt.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.knswz.cn.gov.cn.knswz.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.xzsqb.cn.gov.cn.xzsqb.cn http://www.morning.spqbp.cn.gov.cn.spqbp.cn http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.lskyz.cn.gov.cn.lskyz.cn http://www.morning.bksbx.cn.gov.cn.bksbx.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.ldcrh.cn.gov.cn.ldcrh.cn http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.zqmdn.cn.gov.cn.zqmdn.cn http://www.morning.rfbpq.cn.gov.cn.rfbpq.cn http://www.morning.kqqk.cn.gov.cn.kqqk.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.rbjp.cn.gov.cn.rbjp.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.txysr.cn.gov.cn.txysr.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.trzmb.cn.gov.cn.trzmb.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.nsrtvu.com.gov.cn.nsrtvu.com http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.mhlkc.cn.gov.cn.mhlkc.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.ymfzd.cn.gov.cn.ymfzd.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn