How To Set Up Cache For Xenforo 2

How To Set Up Cache For Xenforo 2

xenforo-2-cache.webp

Cách Thiết Lập Cache Cho Xenforo 2 - How To Set Up Cache For Xenforo 2


Thiết lập xenforo 2 cache giúp tiết kiệm thời gian xử lý và truy vấn cơ sở dữ liệu, giúp diễn đàn của chúng ta load nhanh hơn. Cấu hình cache được lưu trong tệp src/config.php.
Xenforo hỗ trợ nhiều loại cache khác nhau. Ở đây tôi sẽ hướng dẫn bạn 2 loại cache thông dụng nhất, và dễ thực hiện nhất.


Setting up xeforo 2 cache saves time in processing and querying the database, making our forum load faster. The cache configuration is stored in the src/config.php file.
Xenforo supports many different types of caches. Here I will guide you through the two most common types of cache, and the easiest to implement.


1. File system cache
(Lưu cache vào các tập tin trên host của bạn, dễ thực hiện nhất)
(Cache to files on your host, easiest to do)

cpanel-file-manager.webp

Các thực hiện:
  • Truy cập vào Quản lý tập tin của Cpanel, nhận biết được đường dẫn đến thư mục tập tin của mình (như hình trên là /home/user).
  • Tạo 2 thư mục tên cachepage_cache bên trong thư mục internal_data.
  • Lấy đường dẫn đến thư mục đó, thường có dạng như sau: /home/user/public_html/internal_data/cache
  • Mở file src/config.php lên và thêm đoạn code sau đây vào cuối cùng.

  • Access the File Manager of Cpanel, recognize the path to your file directory (as shown above, it is /home/user).
  • Create 2 folders named cache and page_cache inside internal_data folder.
  • Get the path to that directory, usually something like this: /home/user/public_html/internal_data/cache
  • Open the file src/config.php and add the following code at the end.

PHP:
You must log in to view
(8 lines)
  • Lưu ý phải nhập đúng đường dẫn đến thư mục cachepage_cache.
  • Save lại, và truy cập vào web vài lần để thấy hiệu quả.
  • Nếu cấu hình đúng, sẽ thấy trong các thư mục cache tự động phát sinh ra nhiều thư mục con.
  • Khi đã bật cache thì không được xóa các file trong các thư mục cache, nếu không sẽ phát sinh lỗi.
  • Để tắt cache chỉ cần xóa bỏ các dòng đã thêm vào bên trên, sau đó xóa bỏ thư mục cache đã tạo.

  • Note that you must enter the correct path to the cache and page_cache folders.
  • Save, and visit the web a few times to see the effect.
  • If configured correctly, the cache directories will automatically generate many subdirectories.
  • When the cache is enabled, do not delete files in the cache folders, otherwise an error will arise.
  • To disable cache just delete the lines added above, then delete the created cache folder.

2. Memcached
(Memcached giúp tốc độ load nhanh hơn, nhưng phụ thuộc vào nhà cung cấp hosting của bạn, và những host cấu hình yếu không nên dùng)
(Memcached helps to load faster, but depends on your hosting provider, and weakly configured hosts should not be used)

Cách thực hiện:
  • Liên hệ nhà cung cấp hosting xin thông số cấu hình Memcached.
  • Mở file src/config.php lên và thêm đoạn code sau đây vào cuối cùng.

  • Contact your hosting provider for Memcached configuration parameters.
  • Open the file src/config.php and add the following code at the end.
PHP:
You must log in to view
(8 lines)
  • Thông số localhost và 11211 bạn cần liên hệ nhà cung cấp hosting để hỏi.
  • Save lại, và truy cập vào web vài lần để thấy hiệu quả.

  • For localhost and 11211 parameters, you need to contact your hosting provider to ask.
  • Save, and visit the web a few times to see the effect.

Ngoài ra còn nhiều loại cache khác, nhưng nó đòi hỏi host của bạn phải hỗ trợ.
Chỉ sử dụng 1 cấu hình cache, không nên dùng nhiều loại.

There are also many other types of cache, but it requires your host to support.
Use only 1 cache configuration, do not use multiple types.


Ta thấy trong cấu hình cache tôi đã phân thành 2 phần:
  • Phần đầu: Đó là cache chính, sử dụng cho hệ thống và thành viên.
  • Phần sau: Đó là pageCache, sử dụng cho khách truy cập, giúp tối ưu tốc độ load trang. Và chỉ hỗ trợ từ XF 2.1
Khi bạn thay đổi 1 nội dụng bài viết trên diễn đàn. Thành viên truy cập sẽ thấy ngay thay đổi bài viết, còn khách truy cập sẽ thấy dữ liệu cũ trong cache, và khi hết thời gian tồn tại (mặc định là 300 giây), nó sẽ được cập nhật dữ liệu mới. Đây chính là cách để giảm tải cho web khá hiệu quả.

We see in the cache configuration I have divided it into 2 parts:
  • The first part: It is the main cache, used by the system and members.
  • The following part: That is pageCache, used for visitors, to help optimize page load speed. And only support from XF 2.1
When you change 1 post content on the forum. Visitors will see the post changes immediately, visitors will see the old data in the cache, and when it expires (default is 300 seconds), it will be updated with new data. This is a way to reduce the load on the web quite effectively.

Để thay đổi thời gian tồn tại của pageCache, ta thêm vào cuối đoạn mã sau:
(To change the lifetime of pageCache, add the following code at the end)
PHP:
You must log in to view
(1 lines)
Trong đó 300 là số giây pageCache tồn tại, quá thời hạn đó nó sẽ được tạo lại mới.

Hai phần cache này ta có thể cấu hình mỗi phần sử dụng 1 loại cache vẫn được, ví dụ:
(These two cache parts can be configured to use one type of cache each, for example)
PHP:
You must log in to view
(8 lines)

Phần nâng cao (phát triển Add-ons)
Sau khi đã cấu hình thành công cache, nếu bạn đang tự phát triển add-ons cho riêng mình, bạn có thể tận dụng chức năng cache để lưu trữ dữ liệu, giúp giảm tải cho hệ thống, nhất là các truy vấn đến cơ sở dữ liệu.

Advanced section (Add-ons development)
After successfully configuring the cache, if you are developing your own add-ons, you can take advantage of the cache function to store data, helping to reduce the load on the system, especially the queries to the database. database.


Để lưu dữ liệu vào cache ta dùng cú pháp sau:
(To store data in the cache we use the following syntax)
PHP:
You must log in to view
(9 lines)

Để lấy dữ liệu trong cache ra, ta dùng cú pháp sau:
(To get the data out of the cache, we use the following syntax)
PHP:
You must log in to view
(8 lines)



Integrate LiteSpeed Cache for Xenforo 2 in XenVn add-on
LiteSpeed Cache Plugin for XenForo 2 (LSCXF2) is a PHP-based plugin that communicates with your installation of LiteSpeed Web Server(LSWS) and its built-in page cache, LSCache. Because LSCache is built directly into LSWS, overhead is significantly reduced and caching can be done more efficiently than with other PHP-based caches.

XenVn has integrated LiteSpeed Cache support for better performance and security.

Features:

  • Automatic guest page caching
  • Automatic CSS caching
  • Automatic image attachment caching
  • Automatic sitemap, rss caching
  • Automatically update CSRF token
  • Automatically disable caching for ajax requests and important urls
 
Last edited:
1. File system cache
Lưu cache này có tác dụng gì vậy bạn
 
chào bạn,

chỗ phần $config['pageCache']['recordSessionActivity'] mình có nên dùng ko bạn nhỉ :)
 
Top