from django.core.management.base import BaseCommand from django.core.cache import cache from parks.models import Park from rides.models import Ride from analytics.models import PageView class Command(BaseCommand): help = 'Updates trending parks and rides cache based on views in the last 24 hours' def handle(self, *args, **kwargs): """ Updates the trending parks and rides in the cache. This command is designed to be run every hour via cron to keep the trending items up to date. It looks at page views from the last 24 hours and caches the top 10 most viewed parks and rides. The cached data is used by the home page to display trending items without having to query the database on every request. """ # Get top 10 trending parks and rides from the last 24 hours trending_parks = PageView.get_trending_items(Park, hours=24, limit=10) trending_rides = PageView.get_trending_items(Ride, hours=24, limit=10) # Cache the results for 1 hour cache.set('trending_parks', trending_parks, 3600) # 3600 seconds = 1 hour cache.set('trending_rides', trending_rides, 3600) self.stdout.write( self.style.SUCCESS( 'Successfully updated trending parks and rides. ' 'Cached 10 items each for parks and rides based on views in the last 24 hours.' ) )