1、redis简介
在java领域,常见的四大缓存分别是ehcache,memcached,redis,guava-cache,其中redis与其他类型缓存相比,有着得天独厚的优势:
- 它是基于内存的数据库,什么意思呢?由于受磁盘IO影响,它所有操作都在内存当中,用以提高性能,同时采用异步的方式将数据保存在硬盘当中。
- 与memcached相比,redis支持多种数据类型,string,list,set,sorted set,hash。让我们使用起来更加灵活
- 支持事务,操作都是原子性,所谓的原子性就是对数据的更改要么全部执行,要么全部不执行
- 丰富的特性:可用于缓存,消息,可以设置key的过期时间,过期后将会自动删除对应的记录
- redis是单线程单进程的,它利用队列技术将并发访问变为串行访问,消除了传统数据库串行控制的开销。
2、redis常见的数据类型
2.1 redis的key:
我们可以使用任何二进制序列当我们的键,过长过短的键都不建议使用,在这里建议大家使用尝试使用一个模式。例如,“object-type:id”是一个比较推荐的方式,举个例子,我们可以这样来定义键:“user:1000”。点或破折号通常用于多字字段。
2.2 strings: 该类型是最基本的数据类型,也是最常见的数据类型。我们可以使用set 或get 来设置和获取对应的值:
> set mykey somevalue OK > get mykey "somevalue"
在这里面set 如果已经存在的key 会替换已有的值。注意value的值不要大于512M
另外,我们可以使用mset 与 mget来设置和获取多个值,如下:
> mset a 10 b 20 c 30 OK > mget a b c 1) "10" 2) "20" 3) "30"
我们可以使用exists判断key对应的值是否存在,del则是删除key对应的值
> set mykey hello OK > exists mykey (integer) 1 > del mykey (integer) 1 > exists mykey (integer) 0
我们也可以指定key值对应的过期时间:(单位为秒)
> set key some-value OK > expire key 5 (integer) 1 > get key (immediately) "some-value" > get key (after some time) (nil)
2.3 lists类型:这个类似于我们的集合类型,可以存放多个值,我们可以使用lpush(在头部添加)与rpush(在尾部添加)来添加对应的元素:
rpush mylist A (integer) 1 > rpush mylist B (integer) 2 > lpush mylist first (integer) 3 > lrange mylist 0 -1 1) "first" 2) "A" 3) "B"
其中lrange 是取一定范围的元素
2.4 Hashes 这个类似于key->key ,value的形式,我们可以使用hmset与hget来取值
> hmset user:1000 username antirez birthyear 1977 verified 1 OK > hget user:1000 username "antirez" > hget user:1000 birthyear "1977" > hgetall user:1000 1) "username" 2) "antirez" 3) "birthyear" 4) "1977" 5) "verified" 6) "1"
2.5、SETS:存放了一系列无序的字符串集合,我们可以通过sadd与smembers来取值:
> sadd myset 1 2 3 (integer) 3 > smembers myset 1. 3 2. 1 3. 2
3、redis中的事务
MULTI 命令用于开启一个事务,它总是返回 OK 。 MULTI执行之后, 客户端可以继续向服务器发送任意多条命令, 这些命令不会立即被执行, 而是被放到一个队列中, 当 EXEC命令被调用时, 所有队列中的命令才会被执行。redis中事务不存在回滚,我们这么操作试试:
localhost:0>multi "OK" localhost:0>set test abc "QUEUED" localhost:0>incr test "QUEUED" localhost:0>exec 1) "OK" 2) "ERR value is not an integer or out of range" localhost:0>get test "abc"
如果事务当中入队不成功,我们可以看一下例子:
localhost:0>multi "OK" localhost:0>set test abcd "QUEUED" localhost:0>test abc "ERR unknown command 'test'" localhost:0>exec "EXECABORT Transaction discarded because of previous errors." localhost:0>get test "abc"
此时就会终止我们提交事务
另外我们调用 DISCARD , 客户端可以清空事务队列并放弃执行事务。例子:
localhost:0>multi "OK" localhost:0>set test abcd "QUEUED" localhost:0>discard "OK" localhost:0>get test "abc"
4、远程连接redis注意事项
redis现在的版本开启redis-server后,redis-cli只能访问到127.0.0.1,因为在配置文件中固定了ip,因此需要修改redis.conf(有的版本不是这个文件名,只要找到相对应的conf后缀的文件即可)文件以下几个地方。1、bind 127.0.0.1改为 #bind 127.0.0.1 2、protected-mode yes 改为 protected-mode no
指定配置文件运行:
./redis-server ../redis.conf
远程连接的命令:
./redis-cli -p 6379 -h xxx.xxx.xxx
二、使用SpringCache集成redis
1、关于SpringCache
从SpringFramework3.1版本开始,Spring给我们提供了一系列注解和接口规范来简化我们操作缓存代码量,几个核心接口如下:
CacheManager: 该接口主要作用是获取缓存和获取缓存名称
/* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.cache; import java.util.Collection; import org.springframework.lang.Nullable; /** * Spring's central cache manager SPI. * Allows for retrieving named {@link Cache} regions. * * @author Costin Leau * @since 3.1 */ public interface CacheManager { /** * Return the cache associated with the given name. * @param name the cache identifier (must not be {@code null}) * @return the associated cache, or {@code null} if none found */ @Nullable Cache getCache(String name); /** * Return a collection of the cache names known by this manager. * @return the names of all caches known by the cache manager */ Collection<String> getCacheNames(); }