package com.cskaoyan;
import org.redisson.Redisson; import org.redisson.api.*; import org.redisson.config.Config;
import java.util.Iterator; import java.util.List;
/
- @Classname MyRedissionTest
- @Description
- @Author terrance_swn */ public class MyRedissionTest { public static void main(String[] args) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1 创建redission的config对象并配置redis服务器(此处使用singleServer)</span> Config config = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Config(); config.useSingleServer().setAddress(</span>"redis://localhost:6379"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2 创建redis客户端redissionClient</span> RedissonClient redissonClient =<span style="color: rgba(0, 0, 0, 1)"> Redisson.create(config); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3 使用五种数据结构</span>useString(redissonClient);
useHash(redissonClient); useList(redissonClient); useSet(redissonClient); useSortedSet(redissonClient);}
/
- Hash类型
- @param redissonClient */ private static void useHash(RedissonClient redissonClient) {
RMap<Object, Object> map = redissonClient.getMap(“myFirstMap”); map.put(“productId2”,“”);
Object productId2 = map.get(“productId2”); System.out.println(productId2); }
/
- String 数据类型
- @param redissonClient */ private static void useString(RedissonClient redissonClient) {
RBucket<Object> bucket = redissonClient.getBucket(“myString-key”); bucket.set(”{“userName”:“test”,“userPwd”:“test”,“email”:“”,“captcha”:“3552”}”);
Object o = bucket.get(); System.out.println(o); }
/
- List数据类型
- @param redissonClient */ private static void useList(RedissonClient redissonClient) { List<String> list = redissonClient.getList(“listKey-32”); list.add(“listValue1”); list.add(“listValue2”);
String s = list.get(0); System.out.println(s); System.out.println(list); }
/
- Set数据类型
- @param redissonClient */ private static void useSet(RedissonClient redissonClient) { RSet<String> set = redissonClient.getSet(“setKey-32”); set.add(“setValue”);
Iterator<String> iterator = set.iterator(); while (iterator.hasNext()) {
String next </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator.next(); System.out.println(next);} }
/
- Zset数据类型
- @param redissonClient */ private static void useSortedSet(RedissonClient redissonClient) { RScoredSortedSet<String> sortedSet
</span>= redissonClient.getScoredSortedSet("sortedKey-32"<span style="color: rgba(0, 0, 0, 1)">);sortedSet.add(1.0, “zs”); sortedSet.add(2.0, “lisi”);
Double score = sortedSet.getScore(“zs”); System.out.println(score); Integer rank = sortedSet.rank(“zs”); System.out.println(rank); } }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/12516.html