Implementation of the redis class

Source: Internet
Author: User
Tags bitset
Document directory
  • Template or inheritance?
  • New Solution-General Data operations and special basic database operations
  • Generalized Data Operations

Apsaradb for redis provides a series of C interfaces, which are easy to use. However, for some tasks, there is still a lot of code repetition. To solve this code duplication problem, this article will encapsulate these operations.

For redis commands, you can refer to the http://redis.readthedocs.org.

Template or inheritance? This is a problem that has plagued me for many days: If you want to implement a common class library, you need to be compatible with a variety of writing data types, how to unify? If a class template is used, the problem of unified interfaces can be solved. However, the special interface for different data types may be introduced. The result is a monster that needs to implement different operations for each data structure. This design is a nightmare of maintenance and expansion. The new scheme-generalized data operations, special basic database operations after several days of consideration, finally came up with another solution: the base class uses proteced to implement all the basic database operation types, for example:
  • Key)

    • Del
    • Keys
    • Randomkey
    • TTL
    • Pttl
    • Exists
    • Move
    • Rename
    • Renamenx
    • Type
    • Expire
    • Pexpire
    • Expireat
    • Pexpireat
    • Persist
    • Sort
    • Object
    • Migrate

Such operations can be well integrated into the base class without different operations resulting from different data.

For data-related operations, different data may lead to different operation methods, so some general technologies are used for implementation. The general data operation will perform a general analysis based on the different data structures supported by redis. Stringstring is the simplest and most basic data structure in redis, but it is also the most complicated structure in usage. The reason is that the string data type can ensure its binary security, value can store anything, from an image to a movie, as long as it is within the memory limit (512 MB), you are the boss:
  1. String can be simply used as the key-value storage medium (set, mset, get, mget ...)
  2. You can use value to store int and float for related statistics (incr, decr, incrby, decrby, incrbyfloat)
  3. String can be used as a bitset for bitset. (The corresponding commands are getbit and setbit)
This is what redis says on its official website:
  • Use strings as atomic counters using commands in the incr family: incr, decr, incrby. (used as an atomic counter)
  • Append to strings with the APPEND Command. (add data to the end of string)
  • Use strings as a random access vectors with getrange and setrange. (use string as a random access Vector)
  • Encode a lot of data in little space, or create a redis backed bloom Filter Using getbit and setbit. (encoding compression, Bloom filter)

The operations supported in string are as follows: string (string)

  • Set
  • Setnx
  • Setex
  • Psetex
  • Setrange
  • Mset
  • Msetnx
  • Append
  • Get
  • Mget
  • Getrange
  • GetSet
  • Strlen
  • Decr
  • Decrby
  • Incr
  • Incrby
  • Incrbyfloat
  • Setbit
  • Getbit

According to the above analysis, the generalization of interface parameters can be ignored for numeric operations. Integer operations can be directly set to int, and floating point operations can be set to double; however, for operations such as set and mset, to support multiple data structures, we can first abstract the data structure to provide the abstract tostring interface and string2member interface, and constructor whose parameters are string type. In this way, as long as the data structure is used, the set and mset operations can be completed in a multi-state manner (why do we need to provide such operations? The main core idea is to take into account the special feature of the value storage field. A value string can be used to store multiple types of information. Why not use hash? The biggest advantage is that it is simpler than hash-Hash requires multiple values ). Hashhash is also a data structure that every nosql must support. For redis, its storage structure is in the key-filed-value format, that is, the key is followed by the filed, filed is followed by the value. As discussed above, when the data structure to be written is complex, the value data in hash must also be processed by the value in string. Redis supports the following operations: Hash (hash table)

  • Hset
  • Hsetnx
  • Hmset
  • Hget
  • Hmet
  • Hgetall
  • Hdel
  • Hlen
  • Hexists
  • Hincrby
  • Hincrbyfloat
  • Hkeys
  • Hvals

Similarly, in the interface, the key and filed types can be fixed to char *. To support the scalability of value, the type of values accepted by values can be the abstract data structure mentioned above (conversion to string and generation from string are supported ).

Listredis list is a data structure that I personally think is the most elegant. You can use list to implement producer and consumer models (blpop and brpop). You can use list to implement cyclic queues (rpoplpush and brpoplpush ). In concert with the above discussions, the value stored in the data structure is also implemented in the abstract data format described above. List)

  • Lpush
  • Lpushx
  • Rpush
  • Rpushx
  • Lpop
  • Rpop
  • Blpop
  • Brpop
  • Llen
  • Lrange
  • Lrem
  • Lset
  • Ltrim
  • Lindex
  • Linsert
  • Rpoplpush
  • Brpoplpush

At the underlying layer of the setredis set, we calculated it based on the implementation of the list. Set supports intersection, union, and difference set operations. These operations are very effective in statistics. Because you do not care about the data and only care about the key, you can use the built-in data type for the set operation. Set)

  • Sadd
  • Srem
  • Smembers
  • Sismember
  • Scard
  • Smove
  • Spop
  • Srandmember
  • Sinter
  • Sinterstore
  • Sunion
  • Sunionstore
  • Sdiff
  • Sdiffstore
There is a sequent sorted set, and there is a sequent combination of storage formats: Key [score member] [...] the time complexity of adding, deleting, and updating elements to a value and a member is logn. Here, the abstract data member solution mentioned above is still used. Sorted set)

  • Zadd
  • Zrem
  • Zcard
  • Zcount
  • Zscore
  • Zincrby
  • Zrange
  • Zrevrange
  • Zrangebyscore
  • Zrevrangebyscore
  • Zrank
  • Zrevrank
  • Zremrangebyrank
  • Zremrangebyscore
  • Zinterstore
  • Zunionstore

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.