Learn redis applied to cache preparation

 

Learn redis applied to cache (1)

 

1. Install
www.redis.io Install in linux environment
$ wget http://download.redis.io/releases/redis-4.0.1.tar.gz
$ tar xzf redis-4.0.1.tar.gz
$ cd redis-4.0.1
$ make

2. Start the server and client
src/redis-server
src/redis-cli

3. Storage data type
key: string
value: 6 basic value types
  1: string string
  2: hash
  3: string list list
  4: string set set is not repeated, unordered
  5: ordered set sortedset, No repetition, orderly
  6: HyperLogLog structure (only after redis 2.8.9 version, used for cardinality statistics algorithm.)
 
4. Application scenario: use redis as a cache in paging query

Redis database: On the server side of redis, multiple databases (16) are maintained, and the default is 0.
select 0

redis.conf can configure the number of
databases databases 16


Design of redis To convert   
relational    data into KV database, my method is as follows:
key table name: primary key value: column name   
value column value

(1) The value of the method name to be cached is hash
user:{userName}={method name:1}
(2) The value of the data table corresponding to the method name is list
dat:{userName}:{method name}= [result data list ]

exists user:zhangsan
hset user:zhangsan query 1
hexists user:zhangsan   query
del user:zhangsan

exists user:zhangsan:query
llen user:zhangsan:query
rpush user:zhangsan:query 3
lrange user:zhangsan:query 0 -1 [表头到表尾]
ltrim user:zhangsan:query 1 0


5. The java client calls redis
and uses jedis to connect to the redis server: jedis-2.9.0.jar

http://blog.csdn.net/u012658346/article/category/6212310/2

6. Clean up cached data (how to use multithreading to clear invalid data, the cache time is 1 minute)

 

 

7. Problems encountered by
redis redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out

8.匹配key: keys user*

service iptables stop

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326074522&siteId=291194637
Recommended