-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
8 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
springBootBlog/src/main/java/com/yen/mdblog/service/CacheService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.yen.mdblog.service; | ||
|
||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** Cache service created by gpt */ | ||
@Service | ||
public class CacheService { | ||
|
||
/** | ||
* Generic method to cache a result. | ||
* | ||
* @param cacheName the name of the cache | ||
* @param key the cache key | ||
* @param operation the operation to cache | ||
* @param <T> the type of the cached result | ||
* @return the cached result | ||
*/ | ||
@Cacheable(value = "#{cacheName}", key = "#{key}") | ||
public <T> T cacheResult(String cacheName, String key, CacheableOperation<T> operation) { | ||
return operation.execute(); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface CacheableOperation<T> { | ||
T execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters