解释下为什么会有垃圾图片的出现,在上传图片的时候我们选择图片,通常有一个图片的预览和回显,这时候图片已经上传到我们准备的第三方存储或者是自己的服务器,但是会出现最终并没有确认,也就是图片信息并不会存储到数据库,因为没有确认,这时候图片储存在一定的周期一定会出现无效图片,占用内存;
思路:
1.在选择照片时上传到了图片储存我们可以将信息保存一份到redis,这是上传成功的照片的集合;然后将照片的存储地址返回,在前端页面进预览和回显;
2.在点击确认后,将数据保存在数据库,同样确认的照片信息保存一份到redis的集合中,这个集合为确认后的照片信息集合;
3.在一定周期调用处理垃圾图片的服务进行一个清理;利用redis集合的特性,找到这两个集合的差集,也就是无效图片,进一个删除;
以下有代码实现
Quartz定时清理垃圾图片
定时任务组件Quartz
Quartz是Job scheduling(作业调度)领域的一个开源项目,Quartz既可以单独使用也可以跟spring框架整合使用,在实际开发中一般会使用后者。使用Quartz可以开发一个或者多个定时任务,每个定时任务可以单独指定执行的时间,例如每隔1小时执行一次、每个月第一天上午10点执行一次、每个月最后一天下午5点执行一次等
任务调度工具
Quartz: 任务调度框架 常用于定时任务流调度
spring-task:任务调度 常用于单个的定时任务
linux中的 crontab
azkaban: 任务流的调度框架
官网:http://www.quartz-scheduler.org/
1.还是同样的配方,第一步导入依赖
maven坐标
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
2.配置web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.配置applicationContext-redis.xml
<!--Jedis连接池的相关配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal">
<value>200</value>
</property>
<property name="maxIdle">
<value>50</value>
</property>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="127.0.0.1" />
<constructor-arg name="port" value="6379" type="int" />
<constructor-arg name="timeout" value="30000" type="int" />
</bean>
4.配置applicationContext-jobs.xml
<!--开启spring注解使用-->
<context:annotation-config></context:annotation-config>
<!--注册自定义Job-->
<bean id="clearImgJob" class="com.itheima.jobs.ClearImgJob"></bean>
<bean id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 注入目标对象 -->
<property name="targetObject" ref="clearImgJob"/>
<!-- 注入目标方法 -->
<property name="targetMethod" value="clearImg"/>
</bean>
<!-- 注册一个触发器,指定任务触发的时间 -->
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 注入JobDetail -->
<property name="jobDetail" ref="jobDetail"/>
<!-- 指定触发的时间,基于Cron表达式 -->
<property name="cronExpression">
<!--
<value>0 0 2 * * ?</value>
-->
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 注册一个统一的调度工厂,通过这个调度工厂调度任务 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 注入多个触发器 -->
<property name="triggers">
<list>
<ref bean="myTrigger"/>
<ref bean="myTriggerl"/>
</list>
</property>
</bean>
</beans>
(6)创建ClearImgJob定时任务类
package com.itheima.jobs;
import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;
import java.util.Set;
/**
* 自定义Job,实现定时清理垃圾图片
*/
public class ClearImgJob {
@Autowired
private JedisPool jedisPool;
public void clearImg(){
//根据Redis中保存的两个set集合得到差集,获得垃圾图片名称集合
Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
if(set != null){
//得到垃圾图片名称集合遍历得到名称,在进行删除,这里用的是七牛云的图片存储
for (String picName : set) {
//删除七牛云服务器上的图片
QiniuUtils.deleteFileFromQiniu(picName);
//从Redis集合中删除图片名称
jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
System.out.println("自定义任务执行,清理垃圾图片:" + picName);
}
}
}
}