Non-relational database redis

Redis non-relational database.

General as a supplement to the relational database, they demonstrated the advantages of both, to make up for their shortcomings.
There is generally used as a cache.

Installation and Configuration

1. Create a bat file in the installation directory

Content: redis-server.exe redis.windows.conf

2. Set the profile redis.windows.conf in maxheap 1024000000

Data structure (refer to the value in the data structure redis)

string String
hash map corresponding to the set of java, there have key value
data structure list queue
set set, does not allow duplicate elements. Disorderly
sortedset ordered set. Do not allow duplicate elements

command

string

1. Storage: set key value

2. Get: get key

3. Delete: del key

hash

1. Storage: hset key field value

2. Get:

hget key field: obtaining a value corresponding to the specified field
hgetall key: Get all field value and

3. Delete: hdel key field

4. Delete all the del key

list

1. Add:

  1. lpush key value: the list of elements into the left table
  2. rpush key value: the elements are added to the right of the list

    2.lrange key start end: acquiring range

    start: start index
    end: end index
    -1 for get to the end of the list

    3. lpop key: Delete the leftmost list of elements, and the elements return

    rpop key: delete the rightmost list of elements and element returns

    4. Delete all the del key

set

1. Storage: sadd key value

2. Obtain: smembers key: get all the elements in the collection set

3. srem key value: deleting an element set collection

4. Delete all the del key

sortedset

1. Storage: zadd key score value

score smaller, more high ranking

2. 获取:zrange key start end [withscores]

start: start index
end: end index
-1 for obtaining the end of the collection

3. Delete: zrem key value

4. Delete all the del key

Spoken instructions

1. keys *: query all keys

2. type key: obtaining a value corresponding to the key type

Jedis

使用Java代码操作redis。
使用步骤
    1. 导入jar包
    2. 创建Jedis对象
    3. 使用jedis对象调用方法操作redis数据库,方法的名称和redis命令的名称一致
    4. 释放资源
        jedis.close()
setex设置一个有有效期的数据
jedis.zrevrange()倒序查询sortedset

jedis connection pool

JedisPool   
    直接创建对象使用即可
        new JedisPool(config,"localhost",6379)
    获取连接
        Jedis jedis = jedisPool.getResource();

Case

1. 在index.html中,当页面加载完成后,发送异步请求。
2. 在后台Servlet中,获取请求。
3. Servlet调用Service,Service调用DAO
    service调用DAO之前,先从redis中获取数据
4. DAO查询数据库,返回省份列表数据。
5. 依次返回到Servlet后, list需要转为json格式的字符串返回到前台。
6. 在前台index.html中,异步请求的回调函数位置,获取返回的json数据。解析json数据,通过dom操作,把数据展示到页面中。

Cache

前提:
    1. 数据需要经常进行查询
    2. 数据不经常发生变化
策略:
    优先从读取速度比较快的地方获取数据。
        先从redis中拿,如果有,直接返回
                   如果没有,查询数据库,同时把数据存入redis

optimization:

1. 时间换空间
    byte[] bytes = new byte[1024];
2. 空间换时间
3. 空间换空间
4. 时间换时间

Guess you like

Origin www.cnblogs.com/maomaodesu/p/12143050.html