Redis保证事务一致性,以及常用的数据结构
reids命令可以参考中文官网:http://redis.cn/commands.html
关于reids的使用,可以封装到工具类进行调用:
Redis的工具类:JedisAdapter
除了数据结构:reids还可以用来保持事务的一致性;例如: 1:关于Redis的事务:利用reids的exec命令保证执行,不然就discard回滚
例如声明两个方法:
Transaction multi(Jedis jedis)
List<Object> exec(Transaction tx, Jedis jedis)
public Transaction multi(Jedis jedis) {
try {
return jedis.multi();
} catch (Exception e) {
logger.error("发生异常" + e.getMessage());
} finally {
}
return null;
} public List<Object> exec(Transaction tx, Jedis jedis) {
try {
return tx.exec(); //保证事务执行
} catch (Exception e) {
logger.error("发生异常" + e.getMessage());
tx.discard(); //回滚
} finally {
if (tx != null) {
try {
tx.close();
} catch (IOException ioe) {
// ..
}
}
if (jedis != null) {
jedis.close();
}
}
return null;
}
2:对于两个方法的使用:
public boolean follow(int userId, int entityType, int entityId) {
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
Date date = new Date();
// 实体的粉丝增加当前用户
Jedis jedis = jedisAdapter.getJedis();
Transaction tx = jedisAdapter.multi(jedis);
tx.zadd(followerKey, date.getTime(), String.valueOf(userId));
// 当前用户对这类实体关注+1
tx.zadd(followeeKey, date.getTime(), String.valueOf(entityId));
List<Object> ret = jedisAdapter.exec(tx, jedis);
return ret.size() == 2 && (Long) ret.get(0) > 0 && (Long) ret.get(1) > 0;
}
followee/follower两个队列
3:常见的redis数据结构和使用:
List:双向列表,适用于最新列表,关注列表
lpush
lpop
blpop
lindex
lrange
lrem
linsert
lset
rpush
Set:适用于无顺序的集合,点赞点踩,抽奖,已读,共同好友 sdiff
smembers
sinter
scard
SortedSet:排行榜,优先队列 zadd
zscore
zrange
zcount
zrank
zrevrank
Hash:对象属性,不定长属性数 hset
hget
hgetAll
hexists
hkeys
hvals
KV:单一数值,验证码,PV,缓存
set
setex
incr
Jedis jedis = new Jedis("redis://localhost:6379/9");
jedis.flushDB(); // get set
jedis.set("hello", "world");
print(1, jedis.get("hello"));
jedis.rename("hello", "newhello");
print(1, jedis.get("newhello"));
jedis.setex("hello2", 1800, "world"); //
jedis.set("pv", "100");
jedis.incr("pv");
jedis.incrBy("pv", 5);//一次加五
print(2, jedis.get("pv"));
jedis.decrBy("pv", 2);
print(2, jedis.get("pv")); print(3, jedis.keys("*"));//打印出来所有的key //
String listName = "list";
jedis.del(listName);
for (int i = 0; i < 10; ++i) {
jedis.lpush(listName, "a" + String.valueOf(i));
}
print(4, jedis.lrange(listName, 0, 12));
print(4, jedis.lrange(listName, 0, 3));
print(5, jedis.llen(listName));//长度
print(6, jedis.lpop(listName));//先进后出,pop
print(7, jedis.llen(listName));//长度
print(8, jedis.lrange(listName, 2, 6));
print(9, jedis.lindex(listName, 3));
print(10, jedis.linsert(listName, BinaryClient.LIST_POSITION.AFTER, "a4", "xx"));//插入
print(10, jedis.linsert(listName, BinaryClient.LIST_POSITION.BEFORE, "a4", "bb"));//删除
print(11, jedis.lrange(listName, 0 ,12)) ; // hash
String userKey = "userxx";
jedis.hset(userKey, "name", "jim");
jedis.hset(userKey, "age", "12");
jedis.hset(userKey, "phone", "18618181818");
print(12, jedis.hget(userKey, "name"));
print(13, jedis.hgetAll(userKey));
jedis.hdel(userKey, "phone");
print(14, jedis.hgetAll(userKey));
print(15, jedis.hexists(userKey, "email"));
print(16, jedis.hexists(userKey, "age"));
print(17, jedis.hkeys(userKey));
print(18, jedis.hvals(userKey));
jedis.hsetnx(userKey, "school", "zju");
jedis.hsetnx(userKey, "name", "yxy");
print(19, jedis.hgetAll(userKey)); // set集和
String likeKey1 = "commentLike1";
String likeKey2 = "commentLike2";
for (int i = 0; i < 10; ++i) {
jedis.sadd(likeKey1, String.valueOf(i));
jedis.sadd(likeKey2, String.valueOf(i*i));
}
print(20, jedis.smembers(likeKey1)); //smembers(key)取值
print(21, jedis.smembers(likeKey2));
print(22, jedis.sunion(likeKey1, likeKey2));//求并集
print(23, jedis.sdiff(likeKey1, likeKey2)); //补集
print(24, jedis.sinter(likeKey1, likeKey2));//交集
print(25, jedis.sismember(likeKey1, "12"));//判断是否存在
print(26, jedis.sismember(likeKey2, "16"));//判断
jedis.srem(likeKey1, "5");
print(27, jedis.smembers(likeKey1));
jedis.smove(likeKey2, likeKey1, "25"); //从一移进2一个25
print(28, jedis.smembers(likeKey1));
print(29, jedis.scard(likeKey1)); //求总和 String rankKey = "rankKey";//优先队列zset()
jedis.zadd(rankKey, 15, "jim");
jedis.zadd(rankKey, 60, "Ben");
jedis.zadd(rankKey, 90, "Lee");
jedis.zadd(rankKey, 75, "Lucy");
jedis.zadd(rankKey, 80, "Mei");
print(30, jedis.zcard(rankKey));// zcard()输出
print(31, jedis.zcount(rankKey, 61, 100));
print(32, jedis.zscore(rankKey, "Lucy"));
jedis.zincrby(rankKey, 2, "Lucy");
print(33, jedis.zscore(rankKey, "Lucy"));
jedis.zincrby(rankKey, 2, "Luc");
print(34, jedis.zscore(rankKey, "Luc"));
print(35, jedis.zrange(rankKey, 0, 100));
print(36, jedis.zrange(rankKey, 0, 10));//范围输出
print(36, jedis.zrange(rankKey, 1, 3));
print(36, jedis.zrevrange(rankKey, 1, 3));//从低到高排列
for (Tuple tuple : jedis.zrangeByScoreWithScores(rankKey, "60", "100")) {
print(37, tuple.getElement() + ":" + String.valueOf(tuple.getScore()));
} print(38, jedis.zrank(rankKey, "Ben"));
print(39, jedis.zrevrank(rankKey, "Ben")); String setKey = "zset";
jedis.zadd(setKey, 1, "a");
jedis.zadd(setKey, 1, "b");
jedis.zadd(setKey, 1, "c");
jedis.zadd(setKey, 1, "d");
jedis.zadd(setKey, 1, "e"); print(40, jedis.zlexcount(setKey, "-", "+"));
print(41, jedis.zlexcount(setKey, "(b", "[d"));//不包含
print(42, jedis.zlexcount(setKey, "[b", "[d"));//包含,b到d
jedis.zrem(setKey, "b");
print(43, jedis.zrange(setKey, 0, 10));
jedis.zremrangeByLex(setKey, "(c", "+");
print(44, jedis.zrange(setKey, 0 ,2)); /*连接池
JedisPool pool = new JedisPool();
for (int i = 0; i < 100; ++i) {
Jedis j = pool.getResource();
print(45, j.get("pv"));
j.close();
}*/ User user = new User();
user.setName("xx");
user.setPassword("ppp");
user.setHeadUrl("a.png");
user.setSalt("salt");
user.setId(1);
print(46, JSONObject.toJSONString(user));//序列化user
jedis.set("user1", JSONObject.toJSONString(user)); String value = jedis.get("user1");
User user2 = JSON.parseObject(value, User.class);//反序列化,转化为user对象;
print(47, user2);
int k = 2;
Redis保证事务一致性,以及常用的数据结构的更多相关文章
- 2020重新出发,NOSQL,Redis的事务
Redis的基础事务和常用操作 和其他大部分的 NoSQL 不同,Redis 是存在事务的,尽管它没有数据库那么强大,但是它还是很有用的,尤其是在那些需要高并发的网站当中. 使用 Redis 读/写数 ...
- redis的事务不是原子性
Reference: https://blog.csdn.net/u011692780/article/details/81213010 一.事务的四大特性 关系型数据库的事务具有四个特性: 1. 原 ...
- Redis的事务不是原子性的
1.事务的四大特性 原子性(Atomicity):化学中的原子指不可再分的基本微粒,数据库中原子性强调事务是一个不可分割的整体,事务开始后所有操作要么全部成功,要么全部失败,不可能停滞在中间某个环节. ...
- Redis的事务
Redis对事务的支持是部分支持,不想oracle,要么都成功要么都失败,Redis可以部分成功部分失败 1 是什么: 可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺 ...
- Redis笔记(五)Redis的事务
>>关系型数据库的事务 事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消. Atomic(原子性): 一个事务(transaction)中的 ...
- redis之事务
一.是什么 可以一次执行多个命令,本质是一组命令集合.一个事务中的所有命令都会序列化,按顺序的串行化执行而不被其他命令插入,不许加塞.一个队列中,一次性.顺序性.排他性的执行一系列命令. 二.事务常用 ...
- 8、redis之事务1-redis命令
一.概述: 和众多其它数据库一样,Redis作为NoSQL数据库也同样提供了事务机制.在Redis中,MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事务的基石.相信对有 ...
- Redis学习八:Redis的事务
一.是什么 可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺序地串行化执行而不会被其它命令插入,不许加塞. 二.能干嘛 一个队列中,一次性.顺序性.排他性的执行一系列命 ...
- redis(4)事务
一.事务 一般来说,事务必须满足4个条件,也就是我们常说的ACID: 1)Atomicity 原子性:一个事务中的所有操作要么全部完成,要么全部不完成,不会结束在中间的某个环节.事务在执行过程中发生错 ...
随机推荐
- 你必须知道的10个Python第三库
1. BeautifulSoup Beautiful Soup是一个可以从HTML,XML进行提取文件的Python库,日常我们使用爬虫进行数据抓取回来之后,往往需要进行数据解析. 使用它能让你开心愉 ...
- [Swift]LeetCode169. 求众数 | Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [Swift]LeetCode980. 不同路径 III | Unique Paths III
On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. There is e ...
- [Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays
Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping ...
- Python -- socket 实现服务器之间的通信
现在需要做一个分布式课程设计(简单小游戏),三个人小组合作完成. 我需要设计一个登录注册服务器,接收来自网关服务器(消息中间件)的用户登录注册消息请求,然后生成访问数据库服务器的消息,发送给数据库服务 ...
- 如何更简单方便地执行SQL操作?
现在公司使用mybatis作为DAL层的框架. 使用起来比较简单,使用xml进行SQL的书写,java代码使用接口执行. 但在写一些简单SQL的时候会显得非常繁琐: xml和java分离(设计上为了解 ...
- Android--从系统Gallery获取图片
前言 在Android应用中,经常有场景会需要使用到设备上存储的图片,而直接从路径中获取无疑是非常不便利的.所以一般推荐调用系统的Gallery应用,选择图片,然后使用它.本篇博客将讲解如何在Andr ...
- 基于spark实现并行化Apriori算法
详细代码我已上传到github:click me 一. 实验要求 在 Spark2.3 平台上实现 Apriori 频繁项集挖掘的并行化算法.要求程序利用 Spark 进行并行计算. ...
- [七]JavaIO之 PipedInputStream 和 PipedInputStream
管道简介
- HTTP协议简介详解 HTTP协议发展 原理 请求方法 响应状态码 请求头 请求首部 java模拟浏览器客户端服务端
协议简介 协议,自然语言里面就是契约,也是双方或者多方经过协商达成的一致意见; 契约也即类似于合同,自然有甲方123...,乙方123...,哪些能做,哪些不能做; 通信协议,也即是双方通过网络通信必 ...