redis 没有直接提供模糊删除的实现,我们可以根据现有的指令进行组合实现:
import java.util.Arrays; import java.util.Set; import javax.annotation.Resource; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import com.nonobank.application.service.RedisCacheService; @Service("redisCacheService") public class RedisCacheServiceImpl implements RedisCacheService{ //private final Logger logger = LoggerFactory.getLogger(getClass()); @Resource(name = "stringRedisTemplate") private RedisTemplate<String, String> redisTemplate; @Override public void deleteByKey(String key) { redisTemplate.delete(key); } @Override public void deleteByPrex(String prex) { Set<String> keys=redisTemplate.keys(prex+"*"); /*for test *Iterator<String> it=keys.iterator(); * while(it.hasNext()){ logger.info(it.next()); }*/ redisTemplate.delete(keys); } @Override public void deleteBySuffix(String suffix) { Set<String> keys=redisTemplate.keys("*"+suffix); redisTemplate.delete(keys); } @Override public void deleteByKeys(String... keys) { redisTemplate.delete(Arrays.asList(keys)); } }
时间: 2024-10-06 21:58:43