org.springframework.cache.annotation.Cacheable Java Examples

The following examples show how to use org.springframework.cache.annotation.Cacheable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AttachmentService.java    From spring-microservice-exam with MIT License 6 votes vote down vote up
/**
 * 获取附件的预览地址
 *
 * @param attachment attachment
 * @return String
 * @author tangyi
 * @date 2019/06/21 17:45
 */
@Cacheable(value = "attachment_preview#" + CommonConstant.CACHE_EXPIRE, key = "#attachment.id")
public String getPreviewUrl(Attachment attachment) {
	attachment = this.get(attachment);
	if (attachment != null) {
		String preview = attachment.getPreviewUrl();
		if (StringUtils.isNotBlank(preview) && !preview.startsWith("http")) {
			preview = "http://" + preview;
		} else {
			preview = AttachmentConstant.ATTACHMENT_PREVIEW_URL + attachment.getId();
		}
		log.debug("GetPreviewUrl id: {}, preview url: {}", attachment.getId(), preview);
		return preview;
	}
	return "";
}
 
Example #2
Source File: MenuServiceImpl.java    From sk-admin with Apache License 2.0 6 votes vote down vote up
@Override
@Cacheable(key = "'tree'")
public Object getMenuTree(List<Menu> menus) {
    List<Map<String, Object>> list = new LinkedList<>();
    menus.forEach(menu -> {
                if (menu != null) {
                    List<Menu> menuList = menuDao.findByPid(menu.getId());
                    Map<String, Object> map = new HashMap<>(16);
                    map.put("id", menu.getId());
                    map.put("label", menu.getName());
                    if (menuList != null && menuList.size() != 0) {
                        map.put("children", getMenuTree(menuList));
                    }
                    list.add(map);
                }
            }
    );
    return list;
}
 
Example #3
Source File: MenuServiceImpl.java    From yshopmall with Apache License 2.0 6 votes vote down vote up
/**
 * 获取菜单树
 *
 * @param menus /
 * @return /
 */
@Override
@Cacheable(key = "'tree'")
public Object getMenuTree(List<Menu> menus) {
    List<Map<String,Object>> list = new LinkedList<>();
    menus.forEach(menu -> {
                if (menu!=null){
                    List<Menu> menuList = menuMapper.findByPid(menu.getId());
                    Map<String,Object> map = new HashMap<>(16);
                    map.put("id",menu.getId());
                    map.put("label",menu.getName());
                    if(menuList!=null && menuList.size()!=0){
                        map.put("children",getMenuTree(menuList));
                    }
                    list.add(map);
                }
            }
    );
    return list;
}
 
Example #4
Source File: AwsInstanceImageService.java    From greenbot with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("CodeBlock2Expr")
@Override
@Cacheable("AwsInstanceImageService")
public boolean isGreaterThanThreshold(int threshold, Tag includedTag, Tag excludedTag) {

    List<Image> images = regionService.regions()
            .stream()
            .map(region -> Ec2Client.builder().region(region).build())
            .map(client -> query(includedTag, client))
            .flatMap(Collection::stream)
            .filter(image -> {
                if (excludedTag == null)
                    return true;
                return image.tags().stream()
                        .noneMatch(tag -> tag.key().contentEquals(excludedTag.getKey()) && tag.value().contentEquals(excludedTag.getValue()));
            })
            .limit(threshold + 1)
            .collect(Collectors.toList());

    return images.size() > threshold;
}
 
Example #5
Source File: JobServiceImpl.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
@Override
@Cacheable(key = "#p0")
public JobDTO findById(Long id) {
    Job job = jobRepository.findById(id).orElseGet(Job::new);
    ValidationUtil.isNull(job.getId(),"Job","id",id);
    return jobMapper.toDto(job);
}
 
Example #6
Source File: Calculator.java    From Continuous-Delivery-with-Docker-and-Jenkins-Second-Edition with MIT License 5 votes vote down vote up
@Cacheable("sum")
public int sum(int a, int b) {
	try {
		Thread.sleep(3000);
	}
	catch (InterruptedException e) {
		e.printStackTrace();
	}

	return a + b;
}
 
Example #7
Source File: MenuServiceImpl.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
@Override
@Cacheable(key = "#p0")
public MenuDTO findById(long id) {
    Menu menu = menuDao.findById(id).orElseGet(Menu::new);
    ValidationUtil.isNull(menu.getId(), "Menu", "id", id);
    return menuMapper.toDto(menu);
}
 
Example #8
Source File: JeecgDemoServiceImpl.java    From jeecg-boot-with-activiti with MIT License 5 votes vote down vote up
/**
 * 缓存注解测试: redis
 */
@Override
@Cacheable(cacheNames = CacheConstant.TEST_DEMO_CACHE, key = "#id")
public JeecgDemo getByIdCacheable(String id) {
	JeecgDemo t = jeecgDemoMapper.selectById(id);
	System.err.println("---未读缓存,读取数据库---");
	System.err.println(t);
	return t;
}
 
Example #9
Source File: ConstantFactory.java    From MeetingFilm with Apache License 2.0 5 votes vote down vote up
/**
 * 通过角色id获取角色名称
 */
@Override
@Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleId")
public String getSingleRoleName(Integer roleId) {
    if (0 == roleId) {
        return "--";
    }
    Role roleObj = roleMapper.selectById(roleId);
    if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
        return roleObj.getName();
    }
    return "";
}
 
Example #10
Source File: QuartzJobServiceImpl.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
@Override
@Cacheable(key = "#p0")
public QuartzJob findById(Long id) {
    QuartzJob quartzJob = quartzJobDao.findById(id).orElseGet(QuartzJob::new);
    ValidationUtil.isNull(quartzJob.getId(), "QuartzJob", "id", id);
    return quartzJob;
}
 
Example #11
Source File: ProdRegionService.java    From greenbot with Apache License 2.0 5 votes vote down vote up
@Override
@Cacheable("ProdRegionService")
public List<Region> regions() {
    Ec2Client client = Ec2Client.builder().build();

    DescribeRegionsRequest request = DescribeRegionsRequest.builder().allRegions(true).build();
    DescribeRegionsResponse response = client.describeRegions(request);
    return response.regions()
            .stream()
            .filter(re -> !"not-opted-in".equalsIgnoreCase(re.optInStatus()))
            .map(re -> Region.of(re.regionName()))
            .collect(toList());
}
 
Example #12
Source File: UserServiceImpl.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 获取用户
 *
 * @param id key值
 * @return 返回结果
 */
@Cacheable(value = "user", key = "#id")
@Override
public User get(Long id) {
    // 我们假设从数据库读取
    log.info("查询用户【id】= {}", id);
    return DATABASES.get(id);
}
 
Example #13
Source File: FileService.java    From web-flash with MIT License 5 votes vote down vote up
@Override
@Cacheable(value = Cache.APPLICATION, key = "'" + CacheKey.FILE_INFO + "'+#id")
public FileInfo get(Long id){
    FileInfo fileInfo = fileInfoRepository.getOne(id);
    fileInfo.setAblatePath(configCache.get(ConfigKeyEnum.SYSTEM_FILE_UPLOAD_PATH) + File.separator+fileInfo.getRealFileName());
    return fileInfo;
}
 
Example #14
Source File: AdmBasekeyCodeRepository.java    From we-cmdb with Apache License 2.0 4 votes vote down vote up
@Cacheable("basekeyCode-existsById")
boolean existsById(Integer id);
 
Example #15
Source File: SpittleRepository.java    From Project with Apache License 2.0 4 votes vote down vote up
@Cacheable("spittleCache")
List<Spittle> findBySpitterId(long spitterId);
 
Example #16
Source File: AnnotatedClassCacheableService.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Cacheable(cacheNames = "testCache", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
public Object rootVars(Object arg1) {
	return this.counter.getAndIncrement();
}
 
Example #17
Source File: CacheResolverCustomizationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Cacheable(cacheResolver = "namedCacheResolver")
public Object getWithNamedCacheResolution(Object key) {
	return this.counter.getAndIncrement();
}
 
Example #18
Source File: PmsBrandServiceImpl.java    From mall-learning with Apache License 2.0 4 votes vote down vote up
@Cacheable(value = RedisConfig.REDIS_KEY_DATABASE, key = "'pms:brand:'+#id", unless = "#result==null")
@Override
public PmsBrand getItem(Long id) {
    return brandMapper.selectByPrimaryKey(id);
}
 
Example #19
Source File: DefaultCacheableService.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Cacheable(cacheNames = "testCache", unless = "#result > 10")
public Long unless(int arg) {
	return (long) arg;
}
 
Example #20
Source File: DefaultCacheableService.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Cacheable(cacheNames = "testCache", key = "#root.methodName")
public Long name(Object arg1) {
	return this.counter.getAndIncrement();
}
 
Example #21
Source File: DefaultCacheableService.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Cacheable(cacheNames = "testCache", key = "#root.methodName")
public Long name(Object arg1) {
	return counter.getAndIncrement();
}
 
Example #22
Source File: AnnotatedClassCacheableService.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Cacheable(cacheNames = "testCache", keyGenerator = "unknownBeanName")
public Object unknownCustomKeyGenerator(Object arg1) {
	return this.counter.getAndIncrement();
}
 
Example #23
Source File: AnnotatedClassCacheableService.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Cacheable(cacheNames = "testCache", key = "#p0")
public Object key(Object arg1, Object arg2) {
	return counter.getAndIncrement();
}
 
Example #24
Source File: Calculator.java    From Continuous-Delivery-with-Docker-and-Jenkins-Second-Edition with MIT License 4 votes vote down vote up
@Cacheable("sum")
public int sum(int a, int b) {
	return a + b;
}
 
Example #25
Source File: DictServiceImpl.java    From SpringBlade with Apache License 2.0 4 votes vote down vote up
@Override
@Cacheable(cacheNames = DICT_LIST, key = "#code")
public List<Dict> getList(String code) {
	return baseMapper.getList(code);
}
 
Example #26
Source File: DefaultCacheableService.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Cacheable("testCache")
public Long cache(Object arg1) {
	return counter.getAndIncrement();
}
 
Example #27
Source File: CacheService.java    From ultimate-redis-boot with MIT License 4 votes vote down vote up
@Cacheable(cacheNames = "myCache", key = "'myPrefix_'.concat(#relevant)")
public String cacheThis(String relevant, String unrelevantTrackingId){
    log.info("Returning NOT from cache. Tracking: {}!", unrelevantTrackingId);
    return "this Is it";
}
 
Example #28
Source File: CachePutEvaluationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Represent an invalid use case. If the result of the operation is non null, then we put
 * the value with a different key. This forces the method to be executed every time.
 */
@Cacheable
@CachePut(key = "#result + 100", condition = "#result != null")
public Long getAndPut(long id) {
	return this.counter.getAndIncrement();
}
 
Example #29
Source File: DictServiceImpl.java    From SpringBlade with Apache License 2.0 4 votes vote down vote up
@Override
@Cacheable(cacheNames = DICT_VALUE, key = "#code+'_'+#dictKey")
public String getValue(String code, Integer dictKey) {
	return Func.toStr(baseMapper.getValue(code, dictKey), StringPool.EMPTY);
}
 
Example #30
Source File: CacheResolverCustomizationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Cacheable(cacheResolver = "unknownCacheResolver") // No such bean defined
public Object unknownCacheResolver(Object key) {
	return this.counter.getAndIncrement();
}