Comments about the redis

A concept.

redis is a high performance NOSQL series of non-relational database.

Comments about the redis

II. Download and install.

  1, redis a Chinese network.

  2, decompression can be used directly.

    * Redis.windows.conf: configuration file.

    * Redis-cli.exe: redis client.

    * Redis-server.exe: redis server.

III. Command operations.

  1, redis data structure: the format of the key value, key string type, value of the five types:

    * String type string

    * Type hash hash: map format

    * List type list: linkedlist format

    * Set type set

    * Indexed collections sortedset

  2, string type string:

    * Storage: set key value  

    * Get: get key

    * Delete: del key

  3. hash type hash:

    * Storage: hset key field value 

    * 获取: hget key field hgetall key

    * Delete: hdel key field

  4, the list type list: an element may be added to the head or tail of a list. If they add, is to "squeeze" approach.

    * 添加:1、lpush key value 2、rpush key value

    * Get: lrange key start end: the scope of acquisition.

    * Delete: lpop key rpop key delete elements and returns it.

  5, a set of type set: unordered, allowed repeat elements.

    * Storage: sadd key value

     * Get: smembers key Gets a collection of all the elements.

    * Delete; srem key value: Delete a set of elements.

  6, ordered set sortedset: do not allow duplicate elements, but orderly. Sort according score
    * Storage: zadd key score value

    * Get: zrange key start end

    * Delete: zrem key value

  7, Spoken instructions.

    Keys : Search button

    * Type key: get key corresponding to the value of the type.

    * Del key: delete the specified pairs.

Four. Endurance of.

  1, redis is an in-memory database, when redis server is restarted, or restart the computer, data will be lost, so it is necessary to persist to the hard disk

  2, redis persistence mechanism:

    * RDB: default, does not require configuration. Upon detecting the change of key in a certain time, persistent data.  

      Edit redis.windows.conf file:

        save 900 1
        save 300 10
        save 60 10000

        Meaning: how many seconds, at least the number of key changes, and on persistent one.

    * AOF: logging mode, you can record the operation of each command. After each operation command may, persistent data.

      Edit redis.windows.conf file:

        Set appendonly yes (open aof)        

        # Appendfsync always: each operation are persistent
        appendfsync everysec: once every second persistence

        # Appendfsync no: no persistence

Five .Java client Jedis.

  * Jedis: a Java tool operation redis database

  * Use steps: first import jar two packages: jedis and jedis pool

    1, obtaining a connection: Jedis jedis = new Jedis ( "localhost", 6379); Empty default parameters of the machine.

    2. Operation: method name and the same command line.

    3, close the connection: jedis.close ();  

  * Other methods:

    1, jedis.setex ( "activecode", 20, "rt"): key-value pairs stored and automatically deleted after 20 seconds.  

  * Jedis connection pool:

    1, create a configuration object:

      JedisPoolConfig config =new JedisPoolConfig();

      config.setMaxTotal(20) ; config.setMaxIdle(10) ...........

    2, Jedis create a connection pool object.

      JedisPool jp=new JedisPool(config,"localhost",6379)

    3, get connected.

      Jedis jedis=jp.getResource()

    4, use.

    5, returned to the pool. jedis.close ();

  * Custom jedis tools.

  * Redis applications: cache some data does not change often happens with redis.

VI. Case

  Requirements: providing index.html page, there is a drop-down list province, after the page is loaded, sending Ajax, load all provinces.

Guess you like

Origin blog.51cto.com/13954634/2420114