public static function setMostReadCache()
    {
        // check if a article has been read
        if(Cache::has("most_read_articles_ids")) {
            // current date and time
            $currentDate = new DateTime();

            // use to refresh the cache "most_read_articles_ids", removes posts that are older then 2 days
            $mostReadIDs = [];

            // used to make a new array with postID as key end the count (how many times is the post read) as value
            $postByCount = [];

            // get the cached post
            $currentMostReadIDs = Cache::get("most_read_articles_ids");
            foreach($currentMostReadIDs as $key => $post){
                $diff = (int) $currentDate->diff($post['date'])->format("%d");
                if($diff <= 2) {
                    $mostReadIDs[$key] = $post;
                    $postByCount[$key] = $post['count'];
                }
            }

            // save most read id
            Cache::forever("most_read_articles_ids", $mostReadIDs);

            // @TODO cache duhet em u gjeneru prej cron-it, jo çdo here sa te hapet nje artikull me ba kete query
            if(false && $postByCount) {
                // sort posts by count DESC
                arsort($postByCount);

                // get only the 10 most read posts IDs
                $mostReadPostIDs = array_slice(array_keys($postByCount), 0, 10);

                // get post from db
                $post = new Post();
                $post->setTable("post_articles");
                $posts = $post->whereIn("postID", $mostReadPostIDs)->get()->keyBy("postID");

                // make array for cache and arrange the posts with the most read
                $mosReadPosts = [];
                foreach ($mostReadPostIDs as $id) {
                    if (isset($posts[$id])) {
                        $mosReadPosts[$id] = $posts[$id];
                    }
                }

                Cache::forever("most_read_articles", $mosReadPosts);
            }
        }
    }
public static function setMostReadCache()
    {
        // check if a article has been read
        if(Cache::has("most_read_articles_ids")) {
            // current date and time
            $currentDate = new DateTime();

            // use to refresh the cache "most_read_articles_ids", removes posts that are older then 2 days
            $mostReadIDs = [];

            // used to make a new array with postID as key end the count (how many times is the post read) as value
            $postByCount = [];

            // get the cached post
            $currentMostReadIDs = Cache::get("most_read_articles_ids");
            foreach($currentMostReadIDs as $key => $post){
                $diff = (int) $currentDate->diff($post['date'])->format("%d");
                if($diff <= 2) {
                    $mostReadIDs[$key] = $post;
                    $postByCount[$key] = $post['count'];
                }
            }

            // save most read id
            Cache::forever("most_read_articles_ids", $mostReadIDs);

            // @TODO cache duhet em u gjeneru prej cron-it, jo çdo here sa te hapet nje artikull me ba kete query
            if(false && $postByCount) {
                // sort posts by count DESC
                arsort($postByCount);

                // get only the 10 most read posts IDs
                $mostReadPostIDs = array_slice(array_keys($postByCount), 0, 10);

                // get post from db
                $post = new Post();
                $post->setTable("post_articles");
                $posts = $post->whereIn("postID", $mostReadPostIDs)->get()->keyBy("postID");

                // make array for cache and arrange the posts with the most read
                $mosReadPosts = [];
                foreach ($mostReadPostIDs as $id) {
                    if (isset($posts[$id])) {
                        $mosReadPosts[$id] = $posts[$id];
                    }
                }

                Cache::forever("most_read_articles", $mosReadPosts);
            }
        }
    }
public static function setMostReadCache ( ) { // check if a article has been read if ( Cache :: has ( "most_read_articles_ids" ) ) { // current date and time $ currentDate = new DateTime ( ) ; // use to refresh the cache "most_read_articles_ids", removes posts that are older then 2 days $ mostReadIDs = [ ] ; // used to make a new array with postID as key end the count (how many times is the post read) as value $ postByCount = [ ] ; // get the cached post $ currentMostReadIDs = Cache :: get ( "most_read_articles_ids" ) ; foreach ( $ currentMostReadIDs as $ key => $ post ) { $ diff = ( int ) $ currentDate -> diff ( $ post [ 'date' ] ) -> format ( "%d" ) ; if ( $ diff <= 2 ) { $ mostReadIDs [ $ key ] = $ post ; $ postByCount [ $ key ] = $ post [ 'count' ] ; } } // save most read id Cache :: forever ( "most_read_articles_ids" , $ mostReadIDs ) ; // @TODO cache duhet em u gjeneru prej cron-it, jo çdo here sa te hapet nje artikull me ba kete query if ( false && $ postByCount ) { // sort posts by count DESC arsort ( $ postByCount ) ; // get only the 10 most read posts IDs $ mostReadPostIDs = array_slice ( array_keys ( $ postByCount ) , 0 , 10 ) ; // get post from db $ post = new Post ( ) ; $ post -> setTable ( "post_articles" ) ; $ posts = $ post -> whereIn ( "postID" , $ mostReadPostIDs ) -> get ( ) -> keyBy ( "postID" ) ; // make array for cache and arrange the posts with the most read $ mosReadPosts = [ ] ; foreach ( $ mostReadPostIDs as $ id ) { if ( isset ( $ posts [ $ id ] ) ) { $ mosReadPosts [ $ id ] = $ posts [ $ id ] ; } } Cache :: forever ( "most_read_articles" , $ mosReadPosts ) ; } } }
Sets up the cache for the most read articles
Sets up the cache for the most read articles