Skip to content

Commit

Permalink
= 4.2.7.4 =
Browse files Browse the repository at this point in the history
~ Tweak: cache, remove cache some where.
  • Loading branch information
tungnxt89 committed Nov 27, 2024
1 parent f7b9dd1 commit 665ffd0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 94 deletions.
44 changes: 17 additions & 27 deletions inc/Databases/class-lp-course-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,47 +363,37 @@ public function get_total_user_enrolled_or_purchased( int $course_id ): int {
*
* @return null|object
* @throws Exception
* @version 1.0.0
* @version 1.0.1
* @author tungnx
* @since 4.1.4.1
*/
public function get_total_items( int $course_id = 0 ) {
// Get cache
$lp_course_cache = LP_Course_Cache::instance();
$key_cache = "$course_id/total_items";
$total_items = $lp_course_cache->get_cache( $key_cache );

if ( ! $total_items ) {
$item_types = learn_press_get_course_item_types();
$count_item_types = count( $item_types );
$i = 0;

$query_count = $this->wpdb->prepare( 'SUM(s.section_course_id = %d) AS count_items,', $course_id );

foreach ( $item_types as $item_type ) {
++ $i;
if ( $i == $count_item_types ) {
$query_count .= $this->wpdb->prepare( 'SUM(s.section_course_id = %d AND si.item_type = %s) AS %s', $course_id, $item_type, $item_type );
} else {
$query_count .= $this->wpdb->prepare( 'SUM(s.section_course_id = %d AND si.item_type = %s) AS %s,', $course_id, $item_type, $item_type );
}
$item_types = learn_press_get_course_item_types();
$count_item_types = count( $item_types );
$i = 0;

$query_count = $this->wpdb->prepare( 'SUM(s.section_course_id = %d) AS count_items,', $course_id );

foreach ( $item_types as $item_type ) {
++ $i;
if ( $i == $count_item_types ) {
$query_count .= $this->wpdb->prepare( 'SUM(s.section_course_id = %d AND si.item_type = %s) AS %s', $course_id, $item_type, $item_type );
} else {
$query_count .= $this->wpdb->prepare( 'SUM(s.section_course_id = %d AND si.item_type = %s) AS %s,', $course_id, $item_type, $item_type );
}
}

$query = "
$query = "
SELECT $query_count
FROM $this->tb_lp_section_items si
INNER JOIN $this->tb_lp_sections s ON s.section_id = si.section_id
INNER JOIN $this->tb_posts p ON si.item_id = p.ID
AND p.post_status = 'publish'
";

$total_items = $this->wpdb->get_row( $query );

$this->check_execute_has_error();
$total_items = $this->wpdb->get_row( $query );

// Set cache
$lp_course_cache->set_cache( $key_cache, $total_items );
}
$this->check_execute_has_error();

return $total_items;
}
Expand Down
13 changes: 0 additions & 13 deletions inc/Databases/class-lp-user-items-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,6 @@ public function count_status_by_items( LP_User_Items_Filter $filter ) {
* @throws Exception
*/
public function get_last_user_course( LP_User_Items_Filter $filter, bool $force_cache = false ) {
$lp_user_items_cache = new LP_User_Items_Cache();
$key_cache = array(
$filter->user_id,
$filter->item_id,
LP_COURSE_CPT,
);
$result = $lp_user_items_cache->get_user_item( $key_cache );
if ( false !== $result ) {
return json_decode( $result );
}

$query = $this->wpdb->prepare(
"SELECT user_item_id, user_id, item_id, item_type, status, graduation, ref_id, ref_type, start_time, end_time
FROM $this->tb_lp_user_items
Expand All @@ -436,8 +425,6 @@ public function get_last_user_course( LP_User_Items_Filter $filter, bool $force_
$result = $this->wpdb->get_row( $query );

$this->check_execute_has_error();
// Set cache
$lp_user_items_cache->set_user_item( $key_cache, json_encode( $result ) );

return $result;
}
Expand Down
6 changes: 2 additions & 4 deletions inc/Models/CourseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1197,10 +1197,8 @@ public function save(): CourseModel {
$lp_course_json_db->update_data( $data );
}

// Set cache single course when save done.
$key_cache = "courseModel/find/id/{$this->ID}";
$lp_course_cache = new LP_Course_Cache();
$lp_course_cache->set_cache( $key_cache, $this->ID );
// Clear cache
$this->clean_caches();

return $this;
}
Expand Down
17 changes: 2 additions & 15 deletions inc/Models/UserItems/UserItemModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,21 +388,8 @@ public function save(): UserItemModel {
$this->set_user_item_id( $user_item_id_new );
}

// Set caches.
$lp_user_items_cache = new LP_User_Items_Cache();
$lp_user_items_cache->set_user_item(
[
$this->user_id,
$this->item_id,
$this->item_type,
]
);

$key_cache_user_item = "userItemModel/find/{$this->user_id}/{$this->item_id}/{$this->item_type}";
$lp_user_items_cache->set_cache( $key_cache_user_item, $this );

$key_cache_user_item_ref = "userItemModel/find/{$this->user_id}/{$this->item_id}/{$this->item_type}/{$this->ref_id}/{$this->ref_type}";
$lp_user_items_cache->set_cache( $key_cache_user_item_ref, $this );
// Clear caches.
$this->clean_caches();

return $this;
}
Expand Down
34 changes: 0 additions & 34 deletions inc/course/class-lp-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,40 +385,6 @@ public function count_items( string $type = '', bool $include_preview = true ):
}

return 0;

// Get cache
$lp_course_cache = LP_Course_Cache::instance();
$key_cache = "$course_id/total_items";
$total_items = $lp_course_cache->get_cache( $key_cache );
$count_items = 0;

if ( ! $total_items ) {
$extra_info = $this->get_info_extra_for_fast_query();

if ( ! $extra_info->total_items ) {
$total_items = LP_Course_DB::getInstance()->get_total_items( $course_id );
$extra_info->total_items = $total_items;

// Save post meta
$this->set_info_extra_for_fast_query( $extra_info );
} else {
$total_items = $extra_info->total_items;
}

$lp_course_cache->set_cache( $key_cache, $total_items );
}

if ( ! empty( $total_items ) ) {
if ( ! empty( $type ) ) {
if ( isset( $total_items->{$type} ) ) {
$count_items = $total_items->{$type};
}
} else {
$count_items = $total_items->count_items;
}
}

return apply_filters( 'learn-press/course/count-items', intval( $count_items ), $course_id );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ https://www.transifex.com/projects/p/learnpress/
~ Fixed: image cover profile with theme Twenty Five.
~ Tweak order show material on the item lesson.
~ Tweak load text domain compatible with WP 6.7 and later.
~ Tweak: format_human_time_diff.
~ Tweak: format_human_time_diff method.
~ Tweak: UserItemModel, UserCourseModel, UserModel, CourseModel classes.
~ Fixed: error some sites not show notes(Waring) on the Admin Dashboard.
~ Deprecated: get_info_extra_for_fast_query method.
~ Deprecated: _learn_press_usort_terms_by_ID, learn_press_course_item_format_exclude, learn_press_get_course_curriculum, learn_press_is_enrolled_course, learn_press_get_user_course_statuslearn_press_is_free_course, learn_press_course_enroll_required, learn_press_search_post_excerpt, learn_press_course_add_support_item_type, learn_press_course_add_support_item_type, learn_press_get_user_question_answer, need_to_updating, learn_press_get_course_sections, lean_press_get_course_sections, learn_press_get_course_item_url, learn_press_edit_item_link, learn_press_get_course_results_tooltip.
~ Set cache get instructors API for App.
Expand Down

0 comments on commit 665ffd0

Please sign in to comment.