package com.lutao.cloud.gateway.config;
/**
 * Copyright (c) 2022 cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.util.concurrent.TimeUnit;

import com.anji.captcha.service.CaptchaCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

/**

 * @date 2020/8/27
 * <p>
 * 验证码 缓存提供支持集群,需要通过SPI
 */
public class CaptchaCacheServiceProvider implements CaptchaCacheService{
	private static final String REDIS = "redis";

	/**
	 * redis操作字符串
	 */
	@Resource
	private StringRedisTemplate stringRedisTemplate;

	/**
	 * redis 设置字符串值
	 * @param key
	 * @param value
	 * @param expiresInSeconds
	 */
	@Override
	public void set(String key, String value, long expiresInSeconds){
		stringRedisTemplate.opsForValue().set(key, value, expiresInSeconds, TimeUnit.SECONDS);
	}

	/**
	 * 判断key是否存在
	 * @param key
	 * @return
	 */
	@Override
	public boolean exists(String key){
		return stringRedisTemplate.hasKey(key);
	}

	/**
	 * 根据key删除
	 * @param key
	 */
	@Override
	public void delete(String key){
		stringRedisTemplate.delete(key);
	}

	/**
	 * 根据key查询
	 * @param key
	 * @return
	 */
	@Override
	public String get(String key){
		return stringRedisTemplate.opsForValue().get(key);
	}

	/**
	 *
	 * @return
	 */
	@Override
	public String type(){
		return REDIS;
	}

}
