Redis DB Operations

Source: Internet
Author: User

Database Operations

1) Redis is all made up of key and value values, and additions and deletions to the database are based on the mapping of keys to the hash slot and then through the hash slot for a one-way chain traversal to find the value and the specific key.

Also in view of the master wrote the source code can be said to be a good reference:

All the operations here have been subject to source code references are Zai REDIS/SRC/DB.C under the following:

Check

RobJ *lookupkey (Redisdb *db, RobJ *key) {
Dictentry *de = Dictfind (db->dict,key->ptr);
if (DE) {
RobJ *val = Dictgetval (DE);
/* Update the access time for the ageing algorithm.
* Don ' t do it if we had a saving child, as this would trigger
* A copy on write madness. */
if (Server.rdb_child_pid = =-1 && server.aof_child_pid = =-1)
VAL->LRU = Server.lruclock;
return Val;
} Else {
return NULL;
}
}

Lookforkey: Search for value based on key

RobJ is a Redis proposed abstract object class "Of course is written in C, but this is an abstraction layer" contains a series of Redis required parameter data, here through Dictfind find can get Dicentry "is the most basic element of DIC/contains key The value pointer "Constructs a robj that is required.

The middle of the rdb_child_pid and Aof_child_pid is the server to do the persistent cache of 2 of the system, redis through the cow policy to do, if there is a persistent action is done, will go to set the LRU variable this wait for the later persistence, and so on in detail.

Here: There is a detail point: void *val is dicentry a variable, how RobJ *val can get Dicgetval (DE) and also can get the Val of the LRU value?

Because all writes are written through the RobJ object, only the void* type is defined in Dicentry.

This operation is the most basic, no matter how to do additions and deletions need to call Lookforkey

RobJ *lookupkeyread (Redisdb *db, RobJ *key) {
RobJ *val;
Expireifneeded (Db,key);
val = LookupKey (Db,key);
if (val = = NULL)
server.stat_keyspace_misses++;
Else
server.stat_keyspace_hits++;
return Val;
}

Redis is a database that supports the existence of a key value, so it is necessary to do a expireifneed operation on the upper level of writing and reading. This operation is to see if the key expires the time of the key is not the heat in the later part also detailed analysis

Stat_keyspace_misss is to count the total number of keys that do not exist for the entire server, which makes the appropriate decision for the server to analyze the server series of data at a later date

RobJ *lookupkeyreadorreply (redisclient *c, RobJ *key, RobJ *reply) {
RobJ *o = Lookupkeyread (c->db, key);
if (!o) addreply (c,reply);
return o;
}

here is redisclient do abstract if robj is not NULL, then will return a result to the client this result is unified with addreply function to implementWrite

void Dbadd (redisdb *db, RobJ *key, RobJ *val) {
SDS copy = Sdsdup (key->ptr);
int retval = Dictadd (db->dict, Copy, Val);
Redisassertwithinfo (Null,key,retval = = REDIS_OK);
}

This function is the addition operation. By calling the Add function in dic, you can see that all of Redis's Redis is a string composition, and SDS is char*. So the copy stored here is a pointer to a string.

Note: Here the key is copied to copy the, why do this, if the keynote program deleted the key space, but I follow the function: Redis is a storage key and value of the structure, regardless of whether the keynote program frees space, I should exist The key space is released only if the user issues a delete command or the time limit command is reached. The sdsdup here is a process of copying a string.

Process Modification Key
  1. /*-----------------------------------------------------------------------------
  2. * Hooks for key space changes.
  3. *
  4. * Every time a key in the database is modified the function
  5. * Signalmodifiedkey () is called.
  6. *
  7. * Every time a DB is flushed the function signalflushdb () is called.
  8. *----------------------------------------------------------------------------*/
  9. void Signalmodifiedkey (redisdb *db, RobJ *key) {
  10. Touchwatchedkey (Db,key);
  11. }
  12. void signalflusheddb (int dbid) {
  13. Touchwatchedkeysonflush (dbid);
  14. }

This is where the key will be called Touchwatchedkey (Db,key) whenever the key is changed. Why is this function called at Redis every time you make a change?

And where is the specific meaning of Touchwatchedkey?

In the transaction section you can see that whenever the upper-level modification operation is changed, the key will be called Touchwatchedkey () if key is not touched for normal additions and deletions.

If there is, the normal function of the change and deletion, but will do the corresponding redisclient dir rewrite

Redis DB Operations

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.