Redis installation, configuration, program based on redis Learning

Source: Internet
Author: User
Tags benchmark value store

Directory

1. redis Introduction 2. redis installation and configuration 3. Programming Using redis

 

1. redis Introduction

0x1: What is redis?

Redis is a nosql-type key-value-based high-speed cache system,

In terms of architecture, redis has three features

1. key value store is a database stored in the form of key-value. It is located in MySQL and used as a unique storage system. 2. memory Cache is a high-speed cache that stores data in the memory to provide buffering between applications and databases. Instead of memcachd3. data structrue server, memory cache uses it to support high-speed operations on complex data structures as a selling point, provides computing and presentation requirements for some special business scenarios. Such as ranking applications and top 10

In redis's key value "value", it supports the following data structures:

1. string 1) Common commands such as set, get, decr, incr, and mget. 2) in application scenarios, string is the most common data type, common Key/value storage can be classified as this type. 3) the implementation method string is a string by default in redis internal storage, which is referenced by redisobject, when an incr, decr, or other operation is performed, it is converted to a numeric type for calculation. The encoding field of the redisobject is int2. hash 1) Common commands such as hget, hset, and hgetall. 2) Application Scenario 3) implementation Method redis hash corresponds to a hashmap inside the value. Actually, there are two different implementations: 3.1) when there are few Members of this hash, redis will adopt a compact storage like a one-dimensional array to save memory, instead of a real hashmap structure. The value redisobject's encoding is zipmap 3.2) when the number of members increases, it is automatically converted into a real hashmap. In this case, encoding is ht3. List 1) commonly used commands such as lpush, rpush, lpop, rpop, and lrange. 2) application scenarios: redis list has many application scenarios and is also one of the most important data structures of redis. For example, the Twitter follow list and fans list can all be implemented using the redis list structure. 3) implementation Method: The implementation of redis list is a two-way linked list, that is, it supports reverse search and traversal, which is more convenient to operate, but it brings about some additional memory overhead, many internal implementations of redis, this data structure is also used, including the sending Buffer Queue. set 1) Common commands such as sadd, spop, smembers, and sunion. 2) in application scenarios, the functions provided by redis set are similar to those provided by list, set is a good choice when you need to store a list of data and do not want repeated data, in addition, set allows you to determine whether a member is in a set
An important interface in a set, which is not provided by list. 3) implementation method: the internal implementation of set is a hashmap whose value is always null, actually, the hash calculation method is used to quickly remove duplicates. This is also why set can determine whether a member is in the set. 5. sorted set 1) Common commands such as zadd, zrange, zrem, and zcard. 2) Use Cases of redis sorted set are similar to those of set. The difference is that set is not automatically ordered, sorted set can sort members by providing an additional priority (score) parameter. It is inserted in order, that is, automatic sorting. When you need an ordered and non-repeating set
If the list is merged, you can select the sorted set data structure. For example, the Public timeline of Twitter can be stored as the score based on the posting time. In this way, the obtained public timeline is automatically sorted by time. 3) implementation Method redis sorted set uses hashmap and skiplist internally to ensure data storage and order. In hashmap, Members are mapped to scores, the hop table stores all members, and the sorting is based on the score saved in hashmap. The structure of the hop table can be obtained.
High search efficiency and simple implementation

Redis internally uses a redisobject object to represent all keys and values.

1. type indicates the specific data type of a value object. encoding stores different data types in redis. For example, if type = string indicates that value stores a common string, the corresponding encoding can be raw or Int, if it is an int, it indicates that the actual redis internal stores and represents this string by numeric type. Of course, the premise is that
The string itself can be represented by a number, such as a string like "123" "456" 3. PTR Data Pointer 4. VM only enables the redis virtual memory function, this field will actually allocate memory, this function is disabled by default

Redis uses redisobject to indicate that all key/value data is a waste of memory, of course, these memory management costs are mainly used to provide a unified management interface for different redis data types.

Relevant link:

http://try.redis.io/http://www.redis.cn/http://jandyu.diandian.com/post/2012-03-15/16145594http://tech.it168.com/a2011/0818/1234/000001234478_all.shtmlhttp://hedatou.com/archives/introduction_to_redis.html

 

2. install and configure redis

0x1: Main Program Installation

After the CD/usr/localwget http://download.redis.io/releases/redis-2.8.13.tar.gztar xzf redis-2.8.13.tar.gzcd redis-2.8.13make/* make command is executed, a cost executable file is generated in the current src directory (/usr/local/redis-2.8.13/src) as follows: 1. redis-server: Daemon Startup Program of the redis Server 2. redis-CLI: redis command line operation tool. Of course, you can also use Telnet to operate based on its plain text protocol. redis-benchmark: redis performance testing tool to test the read/write performance of redis in your system and your configuration. 4. redis-stat: redis status detection tool that can detect the current status parameters and latency of redis */

0x2: Command Test

// Start the server. /redis-server // test benchmark. /redis-benchmark // use the built-in client to connect to redis. /redis-cli127.0.0.1: 6379> set Foo barok127.0.0.1: 6379> Get foo "bar" 127.0.0.1: 6379>

0x3: Kernel Parameter Optimization required to run redis

/* 1. overcommit_memory specifies the kernel's memory allocation policy. The value can be 0, 1, or 2. 0: indicates that the kernel will check whether there is sufficient available memory for use by the process. 1) if there is enough available memory, the memory application will allow 2) otherwise, the memory application will fail, and return the error to the application process 1: indicates that the kernel allows allocation of all physical memory, regardless of the current memory status 2: indicates that the kernel can allocate more memory than the total physical memory and swap space */Vim/etc/sysctl. conf // Add VM. overcommit_memory = 1 // refresh the configuration to make it take effect for sysctl VM. overcommit_memory = 1

0x4: redis configuration file

Vim/usr/local/redis-2.8.13/redis. conf // 1. enable daemonize Yes // 2. output monitoring information every five seconds (default) daemonize no // 3. reduce the number of changes. This parameter can be set to save 60 1000 // 4. allocate 256000000 MB of memory maxmemory // 5. PID File Location pidfile/var/run/redis. PID // 6. port 6379 // 7. request timeout 0 // 8. log information level: loglevel notice // 9. number of databases enabled: Databases 16/* 10. the Snapshot retention frequency is 1) The first * indicates the time (2) The second * indicates the number of write operations performed within a certain period of time. When a certain number of write operations are performed, the snapshot is automatically saved. You can set multiple conditions */save ** // 11. use compressed rdbcompression yese // 12. the data snapshot file name (only the file name, excluding the Directory) dbfilename dump. RDB // 13. data snapshot storage directory (this is the Directory) dir. /// 14. whether to enable appendonlylog. If it is enabled, a log is recorded for each write operation, which improves data risk resistance, but affects the efficiency of appendonly no/* 15. how to synchronize appendonlylog to disk 1) always: fsync is forcibly called for each write; 2) everysec: fsync is enabled once per second; 3) No: Do not call fsync and wait for system synchronization */appendfsync everysec

After the configuration is saved, restart redis to start normally. Like MySQL, redis provides services based on socket listening ports. We can connect through telnet or socket.

Relevant link:

http://www.redis.cn/documentation.htmlhttp://www.redis.cn/download.htmlhttp://www.php100.com/html/webkaifa/PHP/PHPyingyong/2011/0406/7873.html

 

3. Programming Using redis

0x1: PhP connection to redis

Using PHP to connect to redis requires installing PHP Extension

Download the redis extension source code http://pecl.php.net/package/redisdecompress and compile phpize./configure -- enable-hellomake. For details about PhP extension principles and compilation process, refer to another article titled redis. so/usr/lib/PHP/modules/modify PHP. extension in ini. Add the redis extension to automatically start and restart Apache.

Code

<? PHP $ redis = new redis (); $ result = $ redis-> connect ('2017. 168.207.128 ', 6379); var_dump ($ result); // result: bool (true) $ result = $ redis-> set ('name', "littlehann "); var_dump ($ result); // result: bool (true) $ result = $ redis-> get ('name'); var_dump ($ result); // result: littlehann $ redis-> Delete ('name'); var_dump ($ result); // result: bool (true)?>

Relevant link:

https://github.com/nrk/predishttps://github.com/Shumkov/Rediskahttps://github.com/jdp/redisenthttp://www.cnblogs.com/ikodota/archive/2012/03/05/php_redis_cn.htmlhttp://blog.51yip.com/cache/1439.htmlhttp://www.cnblogs.com/jackluo/p/3412670.html

0x2: connect to redis using Java

Relevant link:

http://outofmemory.cn/code-snippet/128/java-usage-redis-jiandan-usagehttp://www.cnblogs.com/edisonfeng/p/3571870.html

 

Copyright (c) 2014 littlehann All Rights Reserved

 

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.