- Oracle:不开源收费,高帅富;
- SQL Server:不开源收费,微软自家的产品;
- DB2:不开源收费,IBM 的产品;
- Sybase:不开源收费,微软的小基友,关系破裂后家境惨淡;
- MySQL:大家都在用;
- PostgreSQL:学术气息有点重;
- Sqlite:嵌入式数据库,适合桌面和移动应用;
页面级别缓存、对象级别缓存;
页面级别缓存、对象级别缓存;
- ::关闭bai回显,不显示正在执行的批处du理命令及执行的结果等
- @echo off
- echo redis start......
- start "redis_6379" D:\Redis\Redis-6379/redis-server.exe D:\Redis\Redis-6379/redis.windows.conf
- start "redis_6380" D:\Redis\Redis-6380/redis-server.exe D:\Redis\Redis-6380/redis.windows.conf
- start "redis_6381" D:\Redis\Redis-6381/redis-server.exe D:\Redis\Redis-6381/redis.windows.conf
- ::让当前程序进程暂停一下,并显示一行信息:请按任意键继续......
- ::@pause
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- dependency>
- # for Redis
- spring.redis.database=0
- spring.redis.host=127.0.0.1
- spring.redis.port=6379
- #spring.redis.password=111111
- spring.redis.timeout=300
-
- # for Lettuce
- spring.redis.lettuce.pool.max-active=8
- spring.redis.lettuce.pool.max-wait=10000
- spring.redis.lettuce.pool.max-idle=8
- spring.redis.lettuce.pool.min-idle=0
- spring.redis.lettuce.shutdown-timeout=100
- package com.sfac.springBoot.config;
-
- import com.fasterxml.jackson.annotation.JsonAutoDetect;
- import com.fasterxml.jackson.annotation.PropertyAccessor;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.boot.autoconfigure.AutoConfigureAfter;
- import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
- import org.springframework.cache.CacheManager;
- import org.springframework.cache.annotation.CachingConfigurerSupport;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cache.interceptor.KeyGenerator;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.cache.RedisCacheConfiguration;
- import org.springframework.data.redis.cache.RedisCacheManager;
- import org.springframework.data.redis.cache.RedisCacheWriter;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.RedisSerializationContext;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
-
- import java.lang.reflect.Method;
- import java.util.Arrays;
-
- /**
- * @Description Redis Config
- * @Author HymanHu
- * @Date 2021/2/24 13:46
- */
- @Configuration
- @EnableCaching // 启用缓存,使用 Lettuce,自动注入配置的方式
- @AutoConfigureAfter(RedisAutoConfiguration.class)
- public class RedisConfig extends CachingConfigurerSupport{
-
- /**
- * config RedisTemplate
- */
- @Bean
- @SuppressWarnings("all")
- public RedisTemplate
redisTemplate (RedisConnectionFactory factory){ - RedisTemplate
redisTemplate = new RedisTemplate(); - redisTemplate.setConnectionFactory(factory);
-
- // String 序列化方式
- StringRedisSerializer stringSerializer = new StringRedisSerializer();
- // Jackson 序列化方式
- Jackson2JsonRedisSerializer jacksonSerializer = new Jackson2JsonRedisSerializer(Object.class);
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
- objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
- jacksonSerializer.setObjectMapper(objectMapper);
-
- // key 采用 stringSerializer,value 采用 jacksonSerializer
- redisTemplate.setKeySerializer(stringSerializer);
- redisTemplate.setHashKeySerializer(stringSerializer);
- redisTemplate.setValueSerializer(jacksonSerializer);
- redisTemplate.setHashValueSerializer(jacksonSerializer);
-
- return redisTemplate;
- }
-
- /**
- * config CacheManager
- */
- @Bean
- @SuppressWarnings("all")
- public CacheManager cacheManager(LettuceConnectionFactory factory) {
- RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(factory);
- RedisSerializationContext.SerializationPair pair =
- RedisSerializationContext.SerializationPair.fromSerializer(
- new Jackson2JsonRedisSerializer(Object.class));
- RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
- // default set
- // RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
-
- return new RedisCacheManager(writer, config);
- }
-
- // 重新定义缓存 key 的生成策略
- @Bean
- @Override
- public KeyGenerator keyGenerator() {
- 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());
- Arrays.asList(params).stream().forEach(item -> {
- sb.append(item.toString());
- });
- return sb.toString();
- }
- };
- }
- }
用户连续错误登录三次,锁定账户15秒 存在哪里----------redis 存储内容? ------- key:username(唯一标识) +"loginErrorCount" value:连续登录错误次数 如何锁定? /login ----service 取得redis中的key和value,判断value是否大于三次,大于返回错误Result,无需进行登录 小于3次,进shiro认证和资源授权 如何判断连续登录错误3次 catch 代码块: 对redis该key进行自增1的操作,返回错误信息 判断value值是否大于3,如果大于3给该key设置一个过期时间,15秒。
- package com.sfac.springboot.utils;
-
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
-
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
-
- /**
- * @Description Redis Config
- * @Author HymanHu
- * @Date 2021/2/24 13:46
- */
- @Component
- public class RedisUtils {
- private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtils.class);
-
- @Autowired
- private RedisTemplate
redisTemplate; -
- /**
- * count > 0:从左到右删除等于value的第一个元素
- * count = 0:删除等于value的所有元素
- * count < 0:从右到左删除等于value的第一个元素
- */
- public long removeListItem(String key, long count, Object value) {
- try {
- return redisTemplate.opsForList().remove(key, count, value);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return 0;
- }
- }
-
- public boolean updateListItem(String key, long index, Object value) {
- try {
- redisTemplate.opsForList().set(key, index, value);
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public boolean setList(String key, List {
- try {
- redisTemplate.opsForList().rightPushAll(key, list);
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public boolean setListItem(String key, Object value) {
- try {
- redisTemplate.opsForList().rightPush(key, value);
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public Object getListItem(String key, long index) {
- try {
- return redisTemplate.opsForList().index(key, index);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return null;
- }
- }
-
- public long getListSize(String key) {
- try {
- return redisTemplate.opsForList().size(key);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return 0;
- }
- }
-
- public List
- try {
- return redisTemplate.opsForList().range(key, start, end);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return null;
- }
- }
-
- public long removeSetMembers(String key, Object... setValues) {
- try {
- return redisTemplate.opsForSet().remove(key, setValues);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return 0;
- }
- }
-
- public long setSetMembers(String key, long timeOut, Object... values) {
- try {
- long count = redisTemplate.opsForSet().add(key, values);
- if (timeOut > 0) {
- expire(key, timeOut);
- }
- return count;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return 0;
- }
- }
-
- public long setSetMembers(String key, Object... values) {
- try {
- return redisTemplate.opsForSet().add(key, values);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return 0;
- }
- }
-
- public long getSetSize(String key) {
- try {
- return redisTemplate.opsForSet().size(key);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return 0;
- }
- }
-
- public boolean isSetMember(String key, Object setValue) {
- return redisTemplate.opsForSet().isMember(key, setValue);
- }
-
- public Set
- try {
- return redisTemplate.opsForSet().members(key);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return null;
- }
- }
-
- public boolean putHash(String key, Map
hashObject, - long timeOut) {
- try {
- redisTemplate.opsForHash().putAll(key, hashObject);
- if (timeOut > 0) {
- expire(key, timeOut);
- }
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public boolean putHash(String key, Map
hashObject) { - try {
- redisTemplate.opsForHash().putAll(key, hashObject);
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public Map
- return redisTemplate.opsForHash().entries(key);
- }
-
- @SuppressWarnings("all")
- public void deleteHashItems(String key, String... hashKeys) {
- redisTemplate.opsForHash().delete(key, hashKeys);
- }
-
- public boolean haveHahsKey(String key, String hashKey) {
- return redisTemplate.opsForHash().hasKey(key, hashKey);
- }
-
- public double decrementHash(String key, String hashKey, double delta) {
- return redisTemplate.opsForHash().increment(key, hashKey, -delta);
- }
-
- public double incrementHash(String key, String hashKey, double delta) {
- return redisTemplate.opsForHash().increment(key, hashKey, delta);
- }
-
- public boolean putHashValue(String key, String hashKey, Object hashValue, long timeOut) {
- try {
- redisTemplate.opsForHash().put(key, hashKey, hashValue);
- if (timeOut > 0) {
- expire(key, timeOut);
- }
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public boolean putHashValue(String key, String hashKey, Object hashValue) {
- try {
- redisTemplate.opsForHash().put(key, hashKey, hashValue);
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public Object getHashValue(String key, String hashKey) {
- return redisTemplate.opsForHash().get(key, hashKey);
- }
-
- // 设置value递减
- public long decrement(String key, long delta) {
- if (delta < 1) {
- throw new RuntimeException("Increment delta must great than 0.");
- }
- return redisTemplate.opsForValue().decrement(key, delta);
- }
-
- // 设置value递增
- public long increment(String key, long delta) {
- if (delta < 1) {
- throw new RuntimeException("Increment delta must great than 0.");
- }
- return redisTemplate.opsForValue().increment(key, delta);
- }
-
- public boolean set(String key, Object value, long timeOut) {
- try {
- if (timeOut > 0) {
- redisTemplate.opsForValue().set(key, value, timeOut, TimeUnit.SECONDS);
- } else {
- redisTemplate.opsForValue().set(key, value);
- }
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public boolean set(String key, Object value) {
- try {
- redisTemplate.opsForValue().set(key, value);
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public Object get(String key) {
- return StringUtils.isBlank(key) ? null : redisTemplate.opsForValue().get(key);
- }
-
- public void delete(String... key) {
- if (null != key && key.length > 0) {
- redisTemplate.delete(Stream.of(key).collect(Collectors.toList()));
- }
- }
-
- public boolean hasKey(String key) {
- try {
- return redisTemplate.hasKey(key);
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
-
- public long getExpire(String key) {
- return redisTemplate.getExpire(key, TimeUnit.SECONDS);
- }
-
- // 设置过期时间
- public boolean expire(String key, long timeOut) {
- try {
- if (timeOut > 0) {
- redisTemplate.expire(key, timeOut, TimeUnit.SECONDS);
- }
- return true;
- } catch (Exception e) {
- LOGGER.debug(e.getMessage());
- return false;
- }
- }
- }
redis ----key,value
我们会对key和value都进行非空判断,当key为空,直接返回null,不会从redis查询;当value为空(从redis查不到数据) ,设置了默认值为0
流程:
- service 对应的login方法:try中为正确的操作,catch中为错误的操作
-
- //从redis中获得错误次数,超过最大次数,直接返回错误Resutl
- 获得subject
- 包装令牌
- try{
- subject.login();
- subject.checkroles();
- 包装session数据
-
- //把redis里面的错误次数置为0
- 返回正确的result
- }catch{
-
- //错误次数自增1,并且将结果存到redis
-
- //判断错误次数是不是超过最大值,给redis中的key设置过期时间
- 返回错误的result
- }
-
-
-
-