设为首页 加入收藏

TOP

JavaRedisJedis--学习笔记代码片断--序列化(四)
2014-11-24 03:23:40 来源: 作者: 【 】 浏览:13
Tags:JavaRedisJedis-- 学习 笔记 代码 片断 序列化
param object */ public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 反序列化 * @param bytes */ public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return null; } } 启动 redis server:

\
执行序列化代码后,在 redis client中查看结果:

\

IDE控制台输出结果:

\

=====================================================================================

Redis: key-value NoSQL Database
SET server:name "fido"
GET server:name
SET connections 10
INCR connections => 11
INCR connections => 12
DEL connections
INCR connections => 1

DEL to delete a given key and associated value
SET-if-not-exists (called SETNX on Redis)
INCR to atomically increment a number stored at a given key

an atomic operation 原子操作
an atomic operation concepts
h multi-threaded environments
所谓原子操作是指不会被线程调度机制打断的操作;这种操作一旦开始,就一直运行到结束,中间不会有任何 context switch (切换到另一个线程)

a key should only exist for a certain length of time. This is accomplished with the EXPIRE and TTL commands.
key 存活于指定的时间,时间到则销亡
You can test how long a key will exist for with the TTL command
TTL命令: key 还将存活多长时间 返回给定key的剩余生存时间(time to live)(以秒为单位)

complex data structures
数据结构:
list列表:相关命令, RPUSH, LPUSH, LLEN, LRANGE, LPOP, RPOP
RPUSH puts the new value at the end of the list.结尾
RPUSH friends "Alice"

LPUSH puts the new value at the start of the list.开头
LPUSH friends "Sam"

LRANGE gives a subset of the list,给定范围内的值.其中 -1 返回所有的值.
LRANGE friends 0 -1 => ["Sam","Alice","Bob"]
LRANGE friends 0 1 => ["Sam","Alice"]
LRANGE friends 1 2 => ["Alice","Bob"]

LLEN returns the current length of the list.返回当前列表的长度
LLEN friends => 3

LPOP removes the first element from the list and returns it.删除并返回列表中第一个值
LPOP friends => "Sam"

RPOP removes the last element from the list and returns it.删除并返回列表中最后一个值
RPOP friends => "Bob"

Note that the list now only has one element:
LLEN friends => 1
LRANGE friends 0 -1 => ["Alice"]

set集合:相关命令: SADD, SREM, SISMEMBER, SMEMBERS, SUNION
SADD adds the given value to the set.添加
SADD superpowers "flight"
SADD superpowers "x-ray vision"
SADD superpowers "reflexes"

SREM removes the given value from the set.删除
SREM superpowers "reflexes"

SISMEMBER tests if the given value is in the set.查看是否在集合中,也即查看是否是集合的成员
SISMEMBER superpowers "flight" => true,1
SISMEMBER superpowers "reflexes" => false,0

SMEMBERS returns a list of all the members of this set.返回集合中所有的成员,以列表的形式返回.
SMEMBERS superpowers => ["flight","x-ray vision"]

SUNION combines two or more sets and returns the list of all elements.将多个集合合并
SADD birdpowers "pecking"
SADD birdpowers "flight"
SUNION superpowers birdpowers => ["flight","x-ray vision","pecking"]

the sorted set有序集合. It is similar to a regular set, but now each value has an associated score标记.
This score is used to sort the elements in the set.
ZADD hackers 1940 "Alan Kay"
ZADD hackers 1906 "Grace Hopper"
ZADD hackers 1953 "Richard Stallman"
ZADD hackers 1965 "Yukihiro Matsumoto"
ZADD hackers 1916 "Claude Shannon"
ZADD hackers 1969 "Linus Torvalds"
ZADD hackers 1957 "Sophie

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇spring框架学习【多数据源配置】 下一篇函数中有插入语句解决――自治事务

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·用 C 语言或者限制使 (2025-12-25 08:50:05)
·C++构造shared_ptr为 (2025-12-25 08:50:01)
·既然引用计数在做 GC (2025-12-25 08:49:59)
·Java 编程和 c 语言 (2025-12-25 08:19:48)
·. net内存管理宝典这 (2025-12-25 08:19:46)