我在我的Play scala(2.5)应用程序中使用缓存。除了newRecipes之外,我希望所有内容都保持缓存 - 这些我希望每次都能获取新内容。如何从缓存中删除单个项目(newRecipes)?
package controllers
import javax.inject.{Inject, Singleton}
import models.Menu.MainMenus
import models.PrismicAPI.Prismic
import models.Products.{IndivProduct, NewProducts}
import models.Promotions.{RegularPromotions, ShortDatedPromotions}
import play.api.cache.Cached
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.mvc.Controller
@Singleton
class FrontPage @Inject()(cached: Cached
, prismic: Prismic
, newProducts: NewProducts
, recipes: models.Recipes.Recipes
, mainMenu: MainMenus
, regularPromotions: RegularPromotions
, shortDatedPromotions: ShortDatedPromotions
) extends Controller {
def landing(q: Option[String]) = cached("frontpage" + q) {
prismic.action() { implicit request =>
for {
topMenu <- mainMenu.futureMainMenu()
footer <- models.FooterPDFs.futureFooterPDFs()
carouselImages <- models.FrontPage.CarouselImage.futureCarouselImages()
content <- models.FrontPage.FrontPageContent.futureFrontPageContent()
taggedDocs <- models.FrontPage.FrontPage.frontPageTaggedDocs()
newRecipes <- recipes.futureFrontPageRecipes(taggedDocs.filter(_.typ == "recipe"))
newProducts <- newProducts.loadNewProducts(taggedDocs.filter(_.typ == "new-product"))
recentNews <- models.News.NewsArticle.loadNewsArticles(taggedDocs.filter(_.typ == "news-article"))
regularPromos <- regularPromotions.loadRegularPromos(taggedDocs.filter(_.typ == "regular-promotion"))
shortPromos <- shortDatedPromotions.loadShortPromos(taggedDocs.filter(_.typ == "short-dated-promotion"))
} yield {
....
请参阅官方文档以获得更多帮助:https://www.playframework.com/documentation/2.6.x/ScalaCache#Accessing-the-Cache-API
val removeResult: Future[Done] = cache.remove("item.key")