1.redis缓存
1.1.Windows下安装Reids
通过上面的🔗正确安装Redis
1.2.Redis使用
1.注入ReidsTemplate
@Autowired private RedisTemplate redisTemplate;
2.设置Reids的key
//设置redis的key String monthCarCountKey = prefix + "::" + suffix + "::monthCarCount"; Object carCountCache = redisTemplate.opsForValue().get(monthCarCountKey);
3.将数据库中查出来的结果根据(key,value)存储
//将查出来的结果存入reids,设置key对应的value redisTemplate.opsForValue().set(monthCarCountKey, carCount); redisTemplate.opsForValue().set(overWeightCarTotalKey,overWeightCarCount);
4.使用Redis存储的数据
Integer carCount = (Integer) carCountCache; //这里的cachedResult可以自定义数据类型,根据自己存储的cache类型去定义 StatisticsRecord cachedResult = (StatisticsRecord) resultCache; Integer overCarCount = (Integer) overWeightCarTotalCache;
1.3.Redis命令
2.Java-日期时间
2.1.获取当前日期时间
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
// 初始化 Date 对象
Date date = new Date();
// 使用 toString() 函数显示日期时间
System.out.println(date.toString());
}
}
日期方法
1.getTime():获取时间
2.before和after方法
Date1.after(Date2),当Date1大于Date2时,返回TRUE,当小于等于时,返回false;
即Date2比Date1小的true/false,当Date2日期比Date1小的时候为true,否则为falseDate1.before(Date2),当Date1小于Date2时,返回TRUE,当大于等于时,返回false;
2.2.SimpleDateFormat
日期格式和字符串之间的转换
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
//格式化:从Date到String
Date d = new Date();//获取当前系统的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
System.out.println("--------");
//解析:从String到Date
String ss = "2022-03-20 20:20:20";
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(ss);
System.out.println(dd);
}
}
2.3.Calendar类
Calendar类的功能要比Date类强大很多,而且在实现方式上也比Date类要复杂一些。Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,创建对象的过程对程序员来说是透明的,只需要使用getInstance方法创建即可。
//创建一个代表当前日期的Calendar对象sc
Calendar sc = Calendar.getInstance();//默认是当前日期
通过下面的图片可以看出sc有很多方法,按需使用。
Calendar类对象信息的获得
Calendar c1 = Calendar.getInstance();
// 获得年份
int year = c1.get(Calendar.YEAR);
// 获得月份,月份从0开始
int month = c1.get(Calendar.MONTH) + 1;
// 获得日期
int date = c1.get(Calendar.DATE);
// 获得小时
int hour = c1.get(Calendar.HOUR_OF_DAY);
// 获得分钟
int minute = c1.get(Calendar.MINUTE);
// 获得秒
int second = c1.get(Calendar.SECOND);
// 获得星期几(注意(这个与Date类是不同的):1代表星期日、2代表星期1、3代表星期二,以此类推)
int day = c1.get(Calendar.DAY_OF_WEEK);
3.statistics包学习记录
3.1.System.currentTimeMills()
得到了系统的当前时间,返回的是一个长整型的数据
long nowMillis = System.currentTimeMillis();//系统当前时间
long startMillis = nowMillis - 1000L * 60 * 60 * 24 * 90;//往前三个月的时间
this.weightTimeStart = new Date(startMillis);//给开始时间一个默认赋值
this.weightTimeEnd = new Date(nowMillis);//给结束时间一个默认赋值
3.2.二级缓存
二级缓存一般可以用在某个方法上,这样当用户去调用这个方法的时候,便可以直接读取缓存,针对一定的业务逻辑下会有奇效,按需取用
//二级缓存的用法
@Cacheable(cacheNames = "WeightSystem::Statistic::Reader::montCarCount", cacheManager = "cacheManager")