redis是一個key-value存儲系統。和Memcached相似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set –有序集合)和hash(哈希類型)。這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操做,並且這些操做都是原子性的。在此基礎上,redis支持各類不一樣方式的排序。與memcached同樣,爲了保證效率,數據都是緩存在內存中。區別的是redis會週期性的把更新的數據寫入磁盤或者把修改操做寫入追加的記錄文件,而且在此基礎上實現了master-slave(主從)同步。java
Redis數據庫徹底在內存中,使用磁盤僅用於持久性。相比許多鍵值數據存儲,Redis擁有一套較爲豐富的數據類型。Redis能夠將數據複製到任意數量的從服務器。redis
(1)異常快速:Redis的速度很是快,每秒能執行約11萬集合,每秒約81000+條記錄。spring
(2)支持豐富的數據類型:Redis支持最大多數開發人員已經知道像列表,集合,有序集合,散列數據類型。這使得它很是容易解決各類各樣的問題,由於咱們知道哪些問題是能夠處理經過它的數據類型更好。數據庫
(3)操做都是原子性:全部Redis操做是原子的,這保證了若是兩個客戶端同時訪問的Redis服務器將得到更新後的值。windows
(4)多功能實用工具:Redis是一個多實用的工具,能夠在多個用例如緩存,消息,隊列使用(Redis原生支持發佈/訂閱),任何短暫的數據,應用程序,如Web應用程序會話,網頁命中計數等。緩存
(1)單線程bash
(2)耗內存服務器
已經開啓了對應服務的,咱們讓它保持,下面例子須要用到。若是沒有開啓的,咱們命令開啓,進入Redis的安裝目錄(博主的是C:\Program Files\Redis),而後以下命令開啓:app
redis-server redis.windows.conf
若是啓動報錯,請檢查如下配置,maven
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.0.RELEASE</version> </dependency> <!--redis client config--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> <type>jar</type> <scope>compile</scope> </dependency>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.ip}" /> <property name="port" value="${redis.port}" /> <property name="usePool" value="true" /> <property name="poolConfig" ref="jedisPoolConfig" /> <property name="timeout" value="${redis.timeout}"/> </bean> <!-- redisTemplate模板 --> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="kryoRedisSerializer" class="com.supuy.core.util.KryoRedisSerializer"/> <bean id="jdkRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="KeySerializer" ref="stringRedisSerializer" /> <property name="ValueSerializer" ref="stringRedisSerializer" /> <property name="hashKeySerializer" ref="stringRedisSerializer"/> <property name="hashValueSerializer" ref="jdkRedisSerializer"/> </bean> <bean id="redisTransaction" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="KeySerializer" ref="stringRedisSerializer" /> <property name="ValueSerializer" ref="stringRedisSerializer" /> <property name="hashKeySerializer" ref="stringRedisSerializer"/> <property name="hashValueSerializer" ref="jdkRedisSerializer"/> <property name="enableTransactionSupport" value="true"/> </bean>
@Configuration @EnableCaching @ConfigurationProperties(prefix = "spring.redis") public class RedisConfig extends CachingConfigurerSupport{ private String host; private int port; private int timeout; @Bean public KeyGenerator wiselyKeyGenerator(){ return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host); factory.setPort(port); factory.setTimeout(timeout); //設置鏈接超時時間 return factory; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); return cacheManager; } @Bean public RedisTemplate<String, String> redisTemplate() { StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory()); template.setValueSerializer(new StringRedisSerializer()); template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new JdkSerializationRedisSerializer()); return template; } }
@Service public class GeneratorCode { @Autowired protected RedisTemplate redisTemplate; @Autowired private ISysConfigService sysConfigService; /** * 生成通用編號 * * @param myCode the my code * @return the string */ public String generatorCommonCode(String myCode){ String key = "sps_order_code"; String sys_key = "order:totalCode"; ValueOperations<String, String> valueOperations = redisTemplate.opsForValue(); String flowCode = (String)valueOperations.get(key); Long value = Long.valueOf(0); if(flowCode==null){ String item = sysConfigService.getValueByKey(sys_key); valueOperations.set(key, item); }else{ value = Long.valueOf(flowCode)+1; valueOperations.increment(key, 1); sysConfigService.updateSysConfig(sys_key,value.toString()); } String code = Long.valueOf(DateUtils.getUnixTimestamp()+value.longValue()).toString(); return code; } }