商铺项目(Redis缓存)
Posted 虚极静笃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了商铺项目(Redis缓存)相关的知识,希望对你有一定的参考价值。
AOF,RDB是两种 redis持久化的机制。用于crash后,redis的恢复。
两种区别就是,AOF是持续的用日志记录写操作,crash后利用日志恢复;RDB是平时写操作的时候不触发写,只有手动提交save命令,或者是关闭命令时,才触发备份操作。
选择的标准,就是看系统是愿意牺牲一些性能,换取更高的缓存一致性(AOF),还是愿意写操作频繁的时候,不启用备份来换取更高的性能,待手动运行save的时候,再做备份(RDB)。RDB这个就更有些 eventually consistent的意思了。
先讲讲配置:
<!-- redis客户端:Jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Redis连接池的设置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 控制一个pool可分配多少个jedis实例 --> <property name="maxTotal" value="${redis.pool.maxActive}" /> <!-- 连接池中最多可空闲maxIdle个连接 ,这里取值为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态。 --> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <!-- 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间则抛出异常 --> <property name="maxWaitMillis" value="${redis.pool.maxWait}" /> <!-- 在获取连接的时候检查有效性 --> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <!-- 创建Redis连接池,并做相关配置 --> <bean id="jedisWritePool" class="com.ouyan.o2o.cache.JedisPoolWriper" depends-on="jedisPoolConfig"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1" value="${redis.hostname}" /> <constructor-arg index="2" value="${redis.port}" type="int" /> </bean> <!-- 创建Redis工具类,封装好Redis的连接以进行相关的操作 --> <bean id="jedisUtil" class="com.ouyan.o2o.cache.JedisUtil" scope="singleton"> <property name="jedisPool"> <ref bean="jedisWritePool" /> </property> </bean> <!-- Redis的key操作 --> <bean id="jedisKeys" class="com.ouyan.o2o.cache.JedisUtil$Keys" scope="singleton"> <constructor-arg ref="jedisUtil"></constructor-arg> </bean> <!-- Redis的Strings操作 --> <bean id="jedisStrings" class="com.ouyan.o2o.cache.JedisUtil$Strings" scope="singleton"> <constructor-arg ref="jedisUtil"></constructor-arg> </bean> <!-- Redis的Lists操作 --> <bean id="jedisLists" class="com.ouyan.o2o.cache.JedisUtil$Lists" scope="singleton"> <constructor-arg ref="jedisUtil"></constructor-arg> </bean> <!-- Redis的Sets操作 --> <bean id="jedisSets" class="com.ouyan.o2o.cache.JedisUtil$Sets" scope="singleton"> <constructor-arg ref="jedisUtil"></constructor-arg> </bean> <!-- Redis的HashMap操作 --> <bean id="jedisHash" class="com.ouyan.o2o.cache.JedisUtil$Hash" scope="singleton"> <constructor-arg ref="jedisUtil"></constructor-arg> </bean> </beans>
redis.hostname=39.108.63.239 redis.port=6379 redis.database=0 redis.pool.maxActive=100 redis.pool.maxIdle=20 redis.pool.maxWait=3000 redis.pool.testOnBorrow=true
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置整合mybatis过程 --> <!-- 1.配置数据库相关参数properties的属性:${url} --> <bean class="com.ouyan.o2o.util.EncryptPropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:redis.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8" /> </bean> <!-- 2.数据库连接池 --> <bean id="abstractDataSource" abstract="true" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false" /> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000" /> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2" /> </bean> <bean id="master" parent="abstractDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.master.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="slave" parent="abstractDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.slave.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="com.ouyan.o2o.entity" /> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 配置动态数据源,这儿targetDataSources就是路由数据源对应的名称 --> <bean id="dynamicDataSource" class="com.ouyan.o2o.dao.split.DynamicDataSource"> <property name="targetDataSources"> <map> <entry value-ref="master" key="master"></entry> <entry value-ref="slave" key="slave"></entry> </map> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"> <property name="targetDataSource"> <ref bean="dynamicDataSource"/> </property> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.ouyan.o2o.dao" /> </bean> </beans>
package com.ouyan.o2o.cache; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * 强指定redis的JedisPool接口构造函数,这样才能在centos成功创建jedispool * * @author ouyan * */ public class JedisPoolWriper { /** Redis连接池对象 */ private JedisPool jedisPool; public JedisPoolWriper(final JedisPoolConfig poolConfig, final String host, final int port) { try { jedisPool = new JedisPool(poolConfig, host, port); } catch (Exception e) { e.printStackTrace(); } } /** * 获取Redis连接池对象 * @return */ public JedisPool getJedisPool() { return jedisPool; } /** * 注入Redis连接池对象 * @param jedisPool */ public void setJedisPool(JedisPool jedisPool) { this.jedisPool = jedisPool; } }
package com.ouyan.o2o.cache; import java.util.List; import java.util.Map; import java.util.Set; import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.SortingParams; import redis.clients.util.SafeEncoder; public class JedisUtil { /** * 缓存生存时间 */ private final int expire = 60000; /** 操作Key的方法 */ public Keys KEYS; /** 对存储结构为String类型的操作 */ public Strings STRINGS; /** 对存储结构为List类型的操作 */ public Lists LISTS; /** 对存储结构为Set类型的操作 */ public Sets SETS; /** 对存储结构为HashMap类型的操作 */ public Hash HASH; /** Redis连接池对象 */ private JedisPool jedisPool; /** * 获取redis连接池 * * @return */ public JedisPool getJedisPool() { return jedisPool; } /** * 设置redis连接池 * * @return */ public void setJedisPool(JedisPoolWriper jedisPoolWriper) { this.jedisPool = jedisPoolWriper.getJedisPool(); } /** * 从jedis连接池中获取获取jedis对象 * * @return */ public Jedis getJedis() { return jedisPool.getResource(); } /** * 设置过期时间 * * @author ouyan * @param key * @param seconds */ public void expire(String key, int seconds) { if (seconds <= 0) { return; } Jedis jedis = getJedis(); jedis.expire(key, seconds); jedis.close(); } /** * 设置默认过期时间 * * @author ouyan * @param key */ public void expire(String key) { expire(key, expire); } // *******************************************Keys*******************************************// public class Keys { /** * 清空所有key */ public String flushAll() { Jedis jedis = getJedis(); String stata = jedis.flushAll(); jedis.close(); return stata; } /** * 更改key * * @param String * oldkey * @param String * newkey * @return 状态码 */ public String rename(String oldkey, String newkey) { return rename(SafeEncoder.encode(oldkey), SafeEncoder.encode(newkey)); } /** * 更改key,仅当新key不存在时才执行 * * @param String * oldkey * @param String * newkey * @return 状态码 */ public long renamenx(String oldkey, String newkey) { Jedis jedis = getJedis(); long status = jedis.renamenx(oldkey, newkey); jedis.close(); return status; } /** * 更改key * * @param String * oldkey * @param String * newkey * @return 状态码 */ public String rename(byte[] oldkey, byte[] newkey) { Jedis jedis = getJedis(); String status = jedis.rename(oldkey, newkey); jedis.close(); return status; } /** * 设置key的过期时间,以秒为单位 * * @param String * key * @param 时间 * ,已秒为单位 * @return 影响的记录数 */ public long expired(String key, int seconds) { Jedis jedis = getJedis(); long count = jedis.expire(key, seconds); jedis.close(); return count; } /** * 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。 * * @param String * key * @param 时间 * ,已秒为单位 * @return 影响的记录数 */ public long expireAt(String key, long timestamp) { Jedis jedis = getJedis(); long count = jedis.expireAt(key, timestamp); jedis.close(); return count; } /** * 查询key的过期时间 * * @param String * key * @return 以秒为单位的时间表示 */ public long ttl(String key) { // ShardedJedis sjedis = getShardedJedis(); Jedis sjedis = getJedis(); long len = sjedis.ttl(key); sjedis.close(); return len; } /** * 取消对key过期时间的设置 * * @param key * @return 影响的记录数 */ public long persist(String key) { Jedis jedis = getJedis(); long count = jedis.persist(key); jedis.close(); return count; } /** * 删除keys对应的记录,可以是多个key * * @param String * ... keys * @return 删除的记录数 */ public long del(String... keys) { Jedis jedis = getJedis(); long count = jedis.del(keys); jedis.close(); return count; } /** * 删除keys对应的记录,可以是多个key * * @param String * ... keys * @return 删除的记录数 */ public long del(byte[]... keys) { Jedis jedis = getJedis(); long count = jedis.del(keys); jedis.close(); return count; } /** * 判断key是否存在 * * @param String * key * @return boolean */ public boolean exists(String key) { // ShardedJedis sjedis = getShardedJedis(); Jedis sjedis = getJedis(); boolean exis = sjedis.exists(key); sjedis.close(); return exis; } /** * 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法 * * @param String * key * @return List<String> 集合的全部记录 **/ public List<String> sort(String key) { // ShardedJedis sjedis = getShardedJedis(); Jedis sjedis = getJedis(); List<String> list = sjedis.sort(key); sjedis.close(); return list; } /** * 对List,Set,SortSet进行排序或limit * * @param String * key * @param SortingParams * parame 定义排序类型或limit的起止位置. * @return List<String> 全部或部分记录 **/ public List<String> sort(String key, SortingParams parame) { // ShardedJedis sjedis = getShardedJedis(); Jedis sjedis = getJedis(); List<String> list = sjedis.sort(key, parame); sjedis.close(); 以上是关于商铺项目(Redis缓存)的主要内容,如果未能解决你的问题,请参考以下文章