1. 同类产品比较(redis与Memcache)参考来源
- Redis与Memcahe都是将数据放到内存中,都是内存数据库,但是Memcahe还可以混存其他东西,图片、视频等。
- Redis支持的数据类型:String、Hash、List、Set、Zset。
- 虚拟内存--Redis当物理内存用完后,可以将很久没有使用到的value交换到磁盘。
- 过期策略--memcache(set ket 1008:永不过期),Redis通过expire设定(expire name 10)。
- 分布式--设定memcache集群,利用magent做一主多从;redis可以做一主多存,也可以一主一从。
- 存储数据安全--mecache挂掉后,数据没了;redis可持久化,定期保存到磁盘。
- 灾难恢复--memcache挂掉后,数据不可恢复;redis数据丢失后可以 >通过aof恢复。
- Redis支持数据的备份,即master-slave模式的数据备份。
- 应用场景:Redis除了作为Nosql数据库使用,还可以做消息队列、数据堆栈、数据缓存;Memcache适用于缓存sql语句、数据集、用户的临时数据、延迟查询数据与session等。
2. 使用场景 参考来源
-
会话缓存(Session Cache)
Redis提供持久化,信息不会丢失例如购物车。
-
全页缓存(FPC)
除基本的会话token之外,Redis还提供很简便的FPC平台。回到一致性问题,即使重启了Redis实例,因为有磁盘的持久化,用户也不会看到页面加载速度的下降,这是一个极大改进,类似PHP本地FPC。
再次以Magento为例,Magento提供一个插件来使用Redis作为全页缓存后端。
此外,对WordPress的用户来说,Pantheon有一个非常好的插件 wp-redis,这个插件能帮助你以最快速度加载你曾浏览过的页面。 -
队列
Reids在内存存储引擎领域的一大优点是提供 list 和 set 操作,这使得Redis能作为一个很好的消息队列平台来使用。Redis作为队列使用的操作,就类似于本地程序语言(如Python)对 list 的 push/pop 操作。
-
排行榜/计数器
Redis在内存中对数字进行递增或递减的操作实现的非常好。集合(Set)和有序集合(Sorted Set)也使得我们在执行这些操作的时候变的非常简单,Redis只是正好提供了这两种数据结构。所以,我们要从排序集合中获取到排名最靠前的10个用户–我们称之为“user_scores”,我们只需要像下面一样执行即可:
当然,这是假定你是根据你用户的分数做递增的排序。如果你想返回用户及用户的分数,你需要这样执行:
ZRANGE user_scores 0 10 WITHSCORES
Agora Games就是一个很好的例子,用Ruby实现的,它的排行榜就是使用Redis来存储数据的,你可以在<a style="border: 0px; margin: 0px; padding: 0px; font-size: 15px; text-decoration: none; color: rgb(0, 153, 204);">这里</a>看到 发布/订阅
3. 教程
[教程](http://www.yiibai.com/redis/redis_environment.html)
4. springmvc+redis 整合
- maven
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
- Redis setting (redis-context.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- scanner redis properties -->
<context:property-placeholder location="/WEB-INF/property/redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"
p:port="${redis.port}"
p:password="${redis.pass}"
p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
</beans>
- springmvc 文件引入
<!-- 引入同文件夹下的redis属性配置文件 -->
<import resource="redis-context.xml"/>
..... 部分操作