jedis.jar包操作redis服务 kotlin实现

package com.example.demo

import org.junit.Assert
import org.junit.Before
import org.junit.FixMethodOrder
import org.junit.Test
import org.junit.runners.MethodSorters
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.annotation.Rollback
import org.springframework.transaction.annotation.Transactional
import redis.clients.jedis.Jedis
import java.util.*
import java.util.concurrent.ThreadLocalRandom

@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class JedisTest {
    private val jedis = Jedis("localhost")


    @Before
    fun before() {
        //清空所有数据
        val set = jedis.keys("*")
        set.forEach { key ->
            jedis.del(key)
        }
    }

    @Test
    @Rollback
    @Transactional
    fun test001_string() {

        val str = "abcdefghijklmn"
        val key = "key"

        //测试key是否存在
        Assert.assertTrue(!jedis.exists(key))
        Assert.assertEquals(0, jedis.strlen(key))
        Assert.assertEquals(null, jedis.get(key))

        //存放str,并检测是否能正确获取
        jedis.set(key, str)
        Assert.assertEquals(str, jedis.get(key))

        jedis.append(key, str)
        Assert.assertEquals(str.plus(str), jedis.get(key))
    }

    @Test
    fun test002_list() {
        val listKey = "list"

        Assert.assertFalse(jedis.exists("list"))
        val list = mutableListOf<String>()
        for (i in 1..9 step 2) {
            list.add(i.toString())
            jedis.rpush(listKey, i.toString())
        }

        Assert.assertEquals(list, jedis.lrange(listKey, 0.toLong(), list.size.toLong()))
    }

    @Test
    fun test003_map() {
        val mapKey = "map"

        val map = mutableMapOf<String, String>()

        Assert.assertFalse(jedis.exists(mapKey))

        for (i in 1..10) {
            val key = "key".plus(i)
            val value = i.toString()
            jedis.hset(mapKey, key, value)
            map[key] = value
        }

        Assert.assertEquals(map, jedis.hgetAll(mapKey))
        Assert.assertEquals(map.map { entry -> entry.key }.sorted(), jedis.hkeys(mapKey).sorted())
        Assert.assertEquals(map.map { entry -> entry.value }.sorted(), jedis.hvals(mapKey).sorted())
    }

    @Test
    fun test004_set() {
        val setKey = "set"
        val set = mutableSetOf<String>()

        Assert.assertEquals(jedis.scard(setKey), 0.toLong())
        Assert.assertTrue(!jedis.exists(setKey))

        for (i in 1..8) {
            val value = "value".plus(ThreadLocalRandom.current().nextInt())
            jedis.sadd(setKey, value)
            set.add(value)
        }

        println(set)
        println(jedis.smembers(setKey))
        Assert.assertTrue(set.size.toLong() == jedis.scard(setKey))
        Assert.assertEquals(set, jedis.smembers(setKey))
        set.forEach { str -> Assert.assertTrue(jedis.sismember(setKey, str)) }
    }

    @Test
    fun test005_SortedSet() {
        val sortedSetKey = "sortedSet"

        val list = (1..10).shuffled();
        for (i in list) {
            jedis.zadd(sortedSetKey, i.toDouble(), "member".plus(i))
        }
        val set = jedis.zrangeByScoreWithScores(sortedSetKey, Double.MIN_VALUE, Double.MAX_VALUE)
        set.forEach { tuple -> println(tuple) }
    }
}

添加maven依赖

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

 

posted @ 2019-08-09 20:31  笪笠  阅读(568)  评论(0编辑  收藏  举报