This is the 28th day of my participation in the August Text Challenge.More challenges in August

Number of bytes (or bits) of Java types

type Storage (byte) Bit number (bit) Value range
int 4 bytes 4 x 8 bits Minus 2 to the 31st minus 1
short 2 – 2 x 8 bits So minus 2 to the 15th minus 2 to the 15th minus 1
long 8 bytes 8 x 8 bits So minus 2 to the 63rd minus 2 to the 63rd
byte 1 byte 1 x 8 bits So minus 2 to the seventh minus 1, minus 128 to 127
float 4 bytes 4 x 8 bits Values of type float have a suffix F (e.g. 3.14f)
double 8 bytes 8 x 8 bits Floating-point values without the suffix F (e.g. 3.14) default to double
boolean 1 byte 1 x 8 bits True, false,
char 2 – 2 x 8 bits In Java, any character, whether numeric, English or Chinese, takes up two bytes

Note:

  • English numbers, letters, or symbols: 1 character = 1 byte
  • Chinese numbers, letters, or symbols: 1 character = 2 bytes
  • The basic unit of a computer: the bit. A bit represents a 0 or a 1 and one byte is eight bits
  • 1TB=1024GB, 1GB=1024MB, 1MB=1024KB, 1KB=1024B (bytes), 1B=8b (bits)

In Redis, the five data types and application scenarios are as follows:

  • String: cache, counter, distributed lock, etc
  • List: linked List, queue, timeline List of followers on Weibo, etc
  • Hash: user information, Hash table, etc
  • Set: to delike, like, stomp on, share friends, etc
  • Zset: ranking list of page views and clicks, etc

String(String)

String data structures are simple key-value types. Values can be strings as well as numbers (encoding is an integer when numbers are represented by Long, and everything else is stored in SDSHDR as strings).

Hash (dictionary)

In Memcached, we often package structured information into a HashMap that is serialized by the client and stored as a string of values (usually in JSON format), such as the user’s nickname, age, gender, credits, and so on. When you need to modify an item, you usually need to take out the string (JSON), deserialize it, modify the value of an item, and then serialize it into a string (JSON) and store it back. This is a lot of work to do by simply changing a property, which is certainly expensive, and is not suitable for situations where concurrent operations are possible (such as two concurrent operations that require integral changes). Redis’s Hash structure allows you to modify only one attribute value as if you were updating an attribute in the database.

  • Store, read, and modify user attributes

Actual combat scene

  • Cache: The classic usage scenario, the common information, strings, pictures or videos and other information into Redis, Redis as the cache layer, mysql as the persistence layer, reduce mysql read and write pressure
  • Counters: Redis is a single-threaded model, one command is executed before the next, and data can be dropped to other data sources
  • Session: Common solution Spring Session + Redis realizes session sharing

The List (List)

List is simply a linked List (Redis uses a List of double-entailed lists), and anyone who has studied data structures should be able to understand its structure. Using the List structure, we can easily implement functions such as ranking the latest news (such as sina Weibo TimeLine). Another use of lists is message queues, where tasks can be stored in a List using the *PUSH operation of a List, and then pulled out by a worker thread using a POP operation for execution. Redis also provides an API for manipulating elements in a List. You can query and delete elements in a List.

  • Weibo TimeLine
  • The message queue

The technique of using lists

  • Lpush + lpop = Stack (Stack)
  • Lpush +rpop=Queue
  • Lpush + LTRIM =Capped Collection
  • Lpush + brPOP =Message Queue

Actual combat scene:

  • Timeline: For example, the timeline of micro-blog. When someone posts a micro-blog, lpush is used to join the timeline to display the new list information.

Set (Set)

A Set is a Set, and the concept of a Set is a collection of values that are not repeated. Using the Set data structure provided by Redis, you can store some aggregate data. For example, in the microblog application, all the followers of a user can be stored in a collection, and all the fans can be stored in a collection. Because Redis is a personal for collection and intersection, and set, difference set, such as operation, can be very convenient to realize such as mutual concern and common be fond of, two good friends, and other functions, all of the above set operations, you can also use a different command choose to return the results to the client or save to a new collection.

  • Mutual friends, second best friends
  • Using uniqueness, you can count all the individual IP addresses that visit your site
  • When friends recommend, the intersection is calculated according to the tag, and the recommendation can be made if the tag is greater than a certain threshold

Actual combat scene;

  • Tag, you can tag the user, or the user can tag the message, so that the same tag or similar tag can be used to recommend things or people to follow
  • Click like, or click on, favorites, etc., can be put into the set to achieve

Sorted Set

Compared with Sets, Sorted Sets add a weight parameter score to the elements in the Set, so that the elements in the Set can be Sorted according to score. For example, a Sorted Set stores the scores of all students in the class, and its Set value can be the student id of a student. Score can be its test score, so that when the data is inserted into the collection, it is already in a natural order. In addition, Sorted Sets can also be used to perform weighted queues. For example, the score of common messages is 1, and that of important messages is 2. Then, the worker thread can choose to obtain work tasks in reverse order of score. Prioritize important tasks.

  • A weighted element, such as a leaderboard of user scores for a game
  • More complex data structures, generally used in the scene is not too much

Actual combat scene:

  • Leaderboards: An ordered collection of classic usage scenarios. For example, websites such as novel videos need to make a ranking of novel videos uploaded by users. The ranking can be scored according to the number of users’ attention, update time, word count and so on.