今天有兴趣研究redis内部的数据类型,其实从官网的介绍中,redis的数据类型总共有8种,Strings、Lists、Hashes、Sets、Sorted sets、Bitmaps、HyperLogLogs、Streams摘抄官网文档如下:
- Binary-safe strings.
- Lists: collections of string elements sorted according to the order of insertion. They are basically linked lists.
- Sets: collections of unique, unsorted string elements.
- Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements (for example you may ask: give me the top 10, or the bottom 10).
- Hashes, which are maps composed of fields associated with values. Both the field and the value are strings. This is very similar to Ruby or Python hashes.
- Bit arrays (or simply bitmaps): it is possible, using special commands, to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.
- HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set. Don't be scared, it is simpler than it seems... See later in the HyperLogLog section of this tutorial.
- Streams: append-only collections of map-like entries that provide an abstract log data type. They are covered in depth in the Introduction to Redis Streams.
1.String
Redis String类型是可以与Redis键关联的最简单的值类型。 它是Memcached中唯一的数据类型,因此新手在Redis中使用它也很自然。由于Redis键是字符串,当我们使用字符串类型作为值时,我们将字符串映射到另一个字符串。 字符串数据类型对于许多用例很有用,例如缓存HTML片段或页面。
String类型就是在redis中以value为String类型去存储数据对象。
在java中它的操作方法有如下几种
2.List
所谓list数据结构,就是value是list类型,我们可以通过lpush、rpush去存数据,lpop、rpop去取出数据,可以通过lset去修改list某个index下的数据。
example:
list 中index = 2的数据成功被更新为update
具体操作如下
3.set
redis中set数据结构是指,value为set数据结构的数据,其中set是不允许重复的元素出现
有交并差的方法,这里就不做展示了
4.Map
map在redis中就是value为map数据结构的key-value的存在。
5. Sorted set
Sorted set是一个有序的set集合,redis会为这种数据结构的数据集合进行排序。
其他方法这里就不介绍了。
到最后,以上这些就是对redis数据结构的介绍,觉得好的可以点个赞