-redis基本数据类型:
在这里插入图片描述

-string类型常用命令:
1、set name zhangsan 存储 get name 取出
2、setex name 10 zhangsan 存储10秒后消失
3、setnx key1 value1 当key不存在时设置值,如果存在无法覆盖值。

-hash类型常用命令:
1、hset key field value 存储
2、hget key field 取出
3、hdel key field 删除
4、hkeys key 取出所有字段
5、hvals key 取出所有字段值
6、hgetall key 取出所有字段和字段值

-list类型常用命令:
1、lpush key value… 将值插入列表头部
2、lrange key start stop 获取范围内的值
3、rpop key 移除并获取最后一个值
4、llen key 获取列表长度
5、brpop key1 [key2] timeout 移除并获取最后一个值,如果没有元素会阻塞列表到超时或有元素为止

-set类型常用命令(无重复数据):
无序集合:
1、sadd key member1 [member2] 添加成员
2、smembers key 返回成员
3、scard key 返回成员数量
4、sinter key1 [key2] 返回所有集合的交集
5、sunion key1 [key2] 返回所有集合的并集
6、sdiff key1 [key2] 返回所有集合的差集
7、srem key member1 [member2] 移除成员
有序集合:
1、zadd key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的分数
2、zrange key start stop [WITHSCORES] 通过索引区间返回有序集合中指定区间内的成员
3、zincrby key increment member 有序集合中对指定成员的分数加上增量increment
4、zrem key member [member …] 移除有序集合中的一个或多个成员

-通用命令:
1、KEYS pattern 查找所有符合给定模式( pattern)的key (keys *)查看所有key
2、EXISTS key 检查给定key是否存在
3、TYPE key 返回key所储存的值的类型
4、TTL key 返回给定key的剩余生存时间(TTL, time to live),以秒为单位
5、DEL key 该命令用于在key存在是删除key
6、flushall 删除所有key
7、select 1 切换到1号数据库

-在Java中操作redis:
1、添加依赖:

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.0</version>
</dependency>

2、编写代码:

@Test
    public void test(){
        Jedis jedis=new Jedis("localhost",6379);

        jedis.set("username","wyx");

        System.out.println(jedis.get("username"));

        //jedis.del("username");

        jedis.hset("myhash","addr","beijing");

        System.out.println(jedis.hget("myhash", "addr"));

        System.out.println(jedis.keys("*"));

        jedis.close();
    }

3、测试结果:
在这里插入图片描述

-在springboot项目中操作redis:
1、添加依赖:

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.0</version>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、编写配置文件(yml格式):

spring:
  redis:
    database: 0
    host: localhost
    port: 6379
    jedis:
      pool:
        max-active: 8
        max-wait: 1ms
        max-idle: 4
        min-idle: 0
#    password: redis

3、编写配置类,修改序列化器:

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory factory){

        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();

        //更改序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        redisTemplate.setConnectionFactory(factory);

        return redisTemplate;
    }
}

4、编写代码:

@SpringBootTest
@RunWith(SpringRunner.class)
class HelloworldApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void test1() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("city1","hubu");
        valueOperations.set("key1","value1",10l, TimeUnit.SECONDS);//设置超时时间

        System.out.println(valueOperations.setIfAbsent("key1", "value2"));
        System.out.println(valueOperations.get("city1"));
    }

    @Test
    void test2(){
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("001","name","wyx");
        hashOperations.put("001","age","21");
        hashOperations.put("001","addr","bj");
        System.out.println(hashOperations.keys("001"));
        System.out.println(hashOperations.values("001"));
    }

    @Test
    void test3(){
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPush( "mylist", "a");
        listOperations.leftPushAll( "mylist","b", "c", "d");

        System.out.println(listOperations.range("mylist", 0, -1));
    }

    @Test
    void test4() {
        SetOperations setOperations = redisTemplate.opsForSet();

        setOperations.add("myset", "a", "b", "c", "a");
        Set<String> myset = setOperations.members("myset");
        for (String i : myset) {
            System.out.println(i);
        }

        setOperations.remove("myset","a","b");
        myset = setOperations.members("myset");
        for (String i : myset) {
            System.out.println(i);
        }
    }

    @Test
    void test5() {
        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);
        System.out.println(zSetOperations.range("myZset", 0, -1));

        zSetOperations.incrementScore("myZset","a",5.0);
        System.out.println(zSetOperations.range("myZset", 0, -1));

        zSetOperations.remove("myZset","a","b");
        System.out.println(zSetOperations.range("myZset", 0, -1));
    }

    @Test
    void test6() {
        System.out.println(redisTemplate.keys("*"));

        System.out.println(redisTemplate.hasKey("001"));

        System.out.println(redisTemplate.delete("myZset"));

        System.out.println(redisTemplate.type("myList"));
    }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐