NoSql and Redis

1.NoSQL

a) the non-relational database. high write performance, storage will be expired, the data stored in the memory

b) Classification

                     i. the key database

                   ii. Column store database

                  iii. document database
redis and memcached are the key-value Nosql, mainly used for caching

2.Redis

a) high-performance open source Philippine relational database, data is stored in memory or disk inside inside

b) Redis key-value is stored, and the traditional relational database is not the same. Not necessarily follow a few basic requirements for traditional database, for example, do not follow the sql standard, transaction, table structure and so on, redis strictly not a database, it should be for a collection of structured data storage method. Data structures: arrays, list, set, map, etc. redis provides the following methods of operation, we can use these methods to deposit a string, organized into various types of database structures (string, list, set, map, etc.).

Advantages c) Redis database

                     i Data storage: stored in memory, also supports access speed .- persistence, concurrency strong, safe data
also support the cluster (master-slave synchronization) (support high concurrency, mass data), the data may be the master server any number from the synchronization from the server, the server may be associated with another server from the primary server.

                   Type ii. It supports a stored value

  1. String,list,set,sorted set,hash
  2. Cluster Support (support master-slave synchronization)
  3. Support persistence

d) scene using

                     i cache: For frequent data queries, place it inside the Redis will improve query performance, reduce database pressure

                   . Ii counter reference: Number of micro-channel thumbs

                  iii. Real-time offensive and defensive systems

                  iv.             

e) Redis server installation

                     i. Start Redis service

  1. Manual Start: Start Double-click Double-click redis-service.exe in the installation directory
  2. Cmd start:

                   ii. Redis client operating Redis

  1. Value of a string (String) Operation

a) providing a single key: set

b) providing a plurality of keys: mset

  1. The operation of the key

a) Keys: Get all of the key list

b) Del key: Delete key value

c) expire key xx // set the expiration time of the key (xx seconds after expiration)

d) ttl key: Check the expiration time key

e) flushall: clear all data from the database Redis

f) flushdb: Clear the current data library, Redis database default is 16 0,1 ... .15

  1. Operating on a list set
    rdis how to achieve stack (FILO) and queues (the FIFO) ???
    list control into the same side, that is, the same side of the stack
    list while controlling the intake side of the queue is
  2. Set the operation of the collection
  3. Operation of the SortedSet (ordered set) of
  4. Type of hash operations
  5. Redis password

a) Set Password command
CONFIG SET without restarting command can dynamically adjust the configuration Redis server restart failure

CONFIG SET requirepass 123456 // set the password 123456

CONFIG SET requirepass "" // Clear Passwords

AUTH 123456 // password for authentication

b) modify the profile to set a password Redis
increase in the line of code in the configuration file redis.widows.conf

requirepass 123456

123456 Set password to the configuration file, load the file when redis start, you can enable password

3.Java operation Redis server uses client Jedis

a) Jedis simple
introduction jedis package
@Test
public void Test () throws Exception {
    // create a connection Redis server
   
String Host = "127.0.0.1" ;
    int Port = 6379;
    int timeout = 1000; // timeout, 1 second timeout
   
jedis jedis = new new jedis (Host, Port, timeout);
// password authentication
    jedis.auth ( "ADMIN" );
    // . performs operation setting key and value value
   
jedis.set ( "yhptest" , "DBL yhptest ! " );
    System. OUT .println (jedis.get ( " yhptest " ));
    // close the connection
   
jedis.close();
}

b) connection pool configuration

                     . i connected by jedis pool, simple operation Redis database
/ idea: to set a lot of value for him if you create an object, it might as well create configuration objects and finish the configuration, and then create it through configuration object
// create jedispool 1 configuration object
// do 2 configuration - four
// 3 Creating jedispool
// 4 acquired via jedispool
// 5 to perform operations
// 6 release connection
// 7 destroy the connection pool - if it is true it should be a project by spring singleton management
@Test
public void Test () throws Exception {
    //. 1 Create jedispool configuration object
   
JedisPoolConfig config = new new JedisPoolConfig ();
    // 2 do configuration - four
   
config.setMaxIdle (2);
    config.setMaxTotal (10) ;
    config.setMaxWaitMillis (1 * 1000); // create a connection timeout
   
config.setTestOnBorrow ( to true); // get the connection is to test whether the connection is clear
    // 3 Create jedispool
    // 1 * 1000 to get a connection timeout
   
JedisPool the pool = new new JedisPool (config,
            "127.0.0.1" , 6379,1 * 1000, "ADMIN" );
    // 4 obtained by connecting jedispool
   
jedis jedis pool.getResource = ();
    //. 5 performs operations
   
jedis.set ( "jedispooltest" , "dbldblddzt ....." );
    . the System OUT .println (jedis.get ( " jedispooltest " ));
    // 6 release connection
   
jedis.close (); // bottom made compatible, if the connection pool operation is released, if the operation is to close the connection
    // 7 destroy the connection pool - if it is a real project a single embodiment should be managed by the spring
   
pool.destroy ();
}
Summary: Jedis create connection pool configuration objects, and then create a connection pool, the connection pool is created when an incoming connection pool configuration objects, ip address redis storage, Redis port number, set the user connection time-out and operation of Redis, get the connection pool jedis to the object, jedis server object operation. pool and close the connection objects jedis

c) operating a data structure of Jedis

                     i. Key value operation

                   ii. String operations

                  iii. List operations

                  iv. Set operation

                   v. Hash operation

                  vi. Transactions

                 vii. Sort

  1. Digital collection is sorted by calling asc desc and methods
  2. Letter string is sorted by the parameter alpha calling method.
  3. Redis persistence configuration

a) with persistent manner

                     i. RDB and AOF, Redis can be configured by modifying the configuration file conf

                   ii. to start the server execution loader à determine whether to open the persistence of AOF, opened a file on loading the AOF, the file is loaded RDB is not open

                  iii. RDB model

  1. RDB model may be generated in-time snapshots of the data set within a specified time interval, the default mode is on RDB,
  2. How to turn off default on the RDB persistent way: the generated data set at a specified time in Redis.conf time snapshot file inside a comment that if she had a sudden power failure, can not persisted to disk
    save ""

  # Save 900 1 // changed at least once at least 900 seconds is stored in the first synchronization period

  # save xxx

  # save 60 10000

     iv. AOF mode

  1. AOF persistent record of all write commands executed by the server, and when the server starts to restore data sets by re-executing these commands, turn off the default mode.
  2. How to open AOF

a) appendonly yes // yes open, no closed

# Appendfsync always // each time a new write command is executed once synchronized fsync

# Here we enable everysec

fsync appendfsync everysec // sync once per second

# Appendfsync no // Never fsync (to the operating system to handle, might long to perform a fsync), its parameters please see redis.conf Detailed profiles

     v Summary:. Redis is how to save the data.

  1. redis order to consider efficiency, save the data in the content. and consider data security, but also for data persistence, if they meet retention policy, it will save memory data to a data file rdb, had a chance to save part of the data that is stored to update aof log. At load time, and the two make a data set.
  2. Redis elimination strategy: Select the appropriate data elimination

LRU-volatile : expiration time has been set from the data set (server.db [i] .expires choose the least recently used data out) in

volatile-ttl: selection of data to be expired expiration time has been set out from the data set (server.db [i] .expires) in

Random-volatile : expiration time has been set from the data set (server.db [I] .expires ) arbitrarily selected out of the data

allkeys-lru: selection of the least recently used data out from the data set (server.db [i] .dict) in

Random-AllKeys : From the data set (server.db [i] .dict selection data out) any

no-enviction (expulsion): prohibits the expulsion data

After redis determine the expulsion of a key-value pairs, and will delete the data and publish the data change message to the local (AOF persistence) and slave (master-slave connection).

Guess you like

Origin www.cnblogs.com/8888-lhb/p/11520774.html