by Rain Chu | 6 月 30, 2023 | web , wordpress
Redis作為一款開源的高性能key-value數據庫,已經在眾多頂尖科技公司和網站獲得廣泛應用。本文將引領您走過安裝和配置Redis資料庫的過程,並提供實用的優化技巧,助力您的系統性能達到新的高峰。
為什麼選擇Redis? 首先,讓我們理解一下為什麼您應該選擇Redis。Redis以其極速性能、靈活的數據結構和高可用性成為獨一無二的選擇。它可以作為緩存,消息佇列,以及在高壓情況下用作可靠的數據存儲。
安裝Redis Ubuntu安裝Redis sudo apt install redis-server Windows 安裝 Redis 直接下載微軟製作的安裝包,下載網址 https://github.com/MicrosoftArchive/redis/releases
驗證Redis是否安裝成功 如果是剛安裝好,並且在同一台機器上可以直接打指令 redis-cli 就可以連線
redis-cli
redis-cli ping
redis-cli info 如果是別台機器的話,記得要開防火牆,並且指定 IP 以及指定 Port 和指定密碼
redis-cli -h 192.168.0.X -p 6379 -a 123456 設定Redis bind
:若要遠端連入Redis伺服器,就會需要設定 bind 0.0.0.0 ::databases
:設定可用的資料庫數量,在索引的時候是從0
開始數,預設會使用索引值為0的資料庫。這個項目的預設值是16
save
:設定在一定的間隔時間內若資料庫有發生一定程度的改變,就將記憶體中當下的資料存成檔案(快照)。save
的撰寫格式為save <seconds> <changes>
,save 60 10000
表示在60秒內至少有10000個key被改變則做一次快照。requirepass
:設定客戶端與Redis伺服器連線時所需要的密碼。預設沒有設定,表示不啟用密碼驗證功能。maxclients
:設定最大連線數。預設沒有設定,當作10000
。maxmemory
:設定最大的記憶體使用量,如果記憶體用量達到限制,就會根據maxmemory-policy
項目設定的策略來嘗試移除key,如果無法移除,就會使該次插入或修改的操作回傳錯誤。預設沒有設定,表示不限制。maxmemory-policy
:記憶體用量達到限制時採取的策略。預設沒有設定,當作noeviction
,不移除key。其它策略如下:volatile-lru
:根據LRU演算法移除過期的key。allkeys-lru
:根據LRU演算法移除key。(不管有沒有過期)volatile-lfu
:根據LFU演算法移除過期的key。allkeys-lfu
:根據LFU演算法移除key。(不管有沒有過期)volatile-random
:隨機移除過期的key。allkeys-random
:隨機移除key。(不管有沒有過期)volatile-ttl
:移除已過期的key中,TTL最小的key。清除Redis的資料 利用 redis-cli 進去 Redis 主機後,輸入
清除單一資料的指令是
在 WordPress 中使用 Redis Redis Object Cache 外掛網址 https://wordpress.org/plugins/redis-cache/
設定 wp-config.php
// adjust Redis host and port if necessary
define( 'WP_REDIS_HOST', '127.0.0.1' );
define( 'WP_REDIS_PORT', 6379 );
// change the prefix and database for each site to avoid cache data collisions
define( 'WP_REDIS_PREFIX', 'my-moms-site' );
define( 'WP_REDIS_DATABASE', 0 ); // 0-15
// reasonable connection and read+write timeouts
define( 'WP_REDIS_TIMEOUT', 1 );
define( 'WP_REDIS_READ_TIMEOUT', 1 );
/* That's all, stop editing! Happy publishing. */require_once(ABSPATH . 'wp-settings.php'); 組態檔
Configuration constant Default Description WP_REDIS_HOST
127.0.0.1
The hostname of the Redis server WP_REDIS_PORT
6379
The port of the Redis server WP_REDIS_PATH
The path to the unix socket of the Redis server WP_REDIS_SCHEME
tcp
The scheme used to connect:tcp
orunix
WP_REDIS_DATABASE
0
The database used by the cache:0-15
WP_REDIS_PREFIX
The prefix used for all cache keys to avoid data collisions, replacesWP_CACHE_KEY_SALT
. Should be human readable, not a “salt”. WP_REDIS_PASSWORD
The password of the Redis server. Supports Redis ACLs arrays:['user', 'password']
WP_REDIS_MAXTTL
0
The maximum time-to-live of cache keys WP_REDIS_CLIENT
The client used to communicate with Redis:predis
,phpredis
orrelay
WP_REDIS_TIMEOUT
1
The connection timeout in seconds WP_REDIS_READ_TIMEOUT
1
The timeout in seconds when reading/writing WP_REDIS_IGNORED_GROUPS
[]
Groups that should not be cached between requests in Redis
常用控制指令
Command Description wp redis status
Shows the object cache status and diagnostics wp redis enable
Enables the object cache wp redis disable
Disables the object cache wp redis update-dropin
Updates the object cache drop-in
關於Redis的大小事 預設安裝完畢,初始話是支援16個資料庫的,分別由編號 0-15 ,要增加資料庫,要去 Redis 的組態檔裡面修改 redis/redis.conf 中,找到 databases ,並且調整數值後,重新啟動即可
Redis 讀取資料 redis-cli set my_key "Value"
redis-cli get my_key Redis 列出所有的 KEY 相關文章 詳細的指令操作介紹
by Rain Chu | 5 月 22, 2023 | wordpress , 未分類
有需要想要在 WordPress 中紀錄聯絡表單是那一個登入者所寫的,這時候我採用的解決方案是,Contact Form 7 ,在裡面多加兩個欄位,並且用隱藏欄位來做記錄,其中隱藏欄位寫法可以看 https://contactform7.com/hidden-field/ ,可以帶入的參數,則是可以在 https://contactform7.com/setting-default-values-to-the-logged-in-user/ 這邊找的到
最後的完成品如下
[hidden login-name default:user_login]
[hidden user-email default:user_email] by Rain Chu | 3 月 28, 2023 | PHP , woocommerce , wordpress , 程式
許多擁有會員的 wordpress 用戶都希望在用戶登出後能將他們重新定位到特定的頁面,此功能在提供用戶的體驗流程時候非常好用,在登入的時候,以下將提供方法,方便大家可以使用此功能
在 functions.php 撰寫程式碼 可以直接在佈景主題下的 functions.php 中把登出後的導向頁面程式碼加入,加入方法如下
1.找到 wp-content 然後 themes 和你現在在使用的主題文件夾
2. 編輯 functions.php
3.加入以下代碼
3.1 登出後直接導引到首頁
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_safe_redirect( home_url() );
exit();
} 3.2 登出後導引到特定頁面
add_action('wp_logout','auto_redirect_external_after_logout');
function auto_redirect_external_after_logout(){
wp_redirect( 'https://www.yourwebsite.com' );
exit();
} 3.3 採用 add_filter 來實作導引到特定頁面
function custom_logout_redirect($logout_url) {
$redirect_page = 'https://yourwebsite.com/your-redirect-page/';
return add_query_arg('redirect_to', urlencode($redirect_page), $logout_url);
}
add_filter('logout_url', 'custom_logout_redirect');
其中 yourwebsite.com 要置換成你自己想要導引過去的頁面
4.將 functions.php 存檔並且關閉
之後就可以自定義客戶登出的行為了
參考資料
by Rain Chu | 3 月 21, 2023 | PHP , wordpress , 程式
想要客製化會員登入 WordPress 後的行為,例如說要跳到某一個指定的網頁,其實有很多個外掛可以使用,例如:https://www.paidmembershipspro.com/ ,但麻煩的就是常常會跟其他的會員外掛衝突,而且為了一個小功能,要一個龐大的會員系統來支撐,不太划算,建議可以自行寫寫看。
佈景主題 Functions.php 其實只要找到佈景主題下的 functions.php 然後加入以下程式碼即可
function my_login_redirect( $redirect_to, $request, $user ) {
//檢查是否有建置會員制度
if (isset($user->roles) && is_array($user->roles)) {
// 檢查是否為管理者
if(in_array('administrator', $user->roles)){
// 每次當入都到 woocommerce 的管理頁面中
$redirect_to = home_url('wp-admin/edit.php?post_type=shop_order');
}
//確認是不是訂閱會員
else if (in_array('subscriber', $user->roles)) {
// 導向指定頁面,程式範例是導向會員專屬頁面。
$redirect_to = home_url();
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 ); 區分權限 管理員登入 WordPress 後,預設會到 /wp-admin 中,一般的使用者登入後通常是引導到首頁,或是他自己的介紹頁,要區分管理員或是一般使用者則可以使用 in_array(‘administrator’, $user->roles) 做判斷
// 判斷是否為管理員
in_array('administrator', $user->roles) 自己寫就是有一個好處,可以完全客制使用者行為,也比較輕量,真的有問題就問 ChatGPT 4 大神吧
參考資料 by Rain Chu | 3 月 14, 2023 | PHP , wordpress , 程式
如果需要在網站上固定執行一個程式碼,有非常多種的執行方法,本篇已 WordPress 為例,分別在不同的層級上介紹幾個大家常用的方法來說明,可以先簡單分類,由 WordPress (應用層)去執行的,Web Server 等級去執行的,PHP 層級執行的,原生 Server 等級去執行,外部執行,自己寫一隻程式執行,各有各的美,以上排序由簡->難。
首先介紹最簡單的外掛處理法 WP Control ,可以節省很多事情,有優良的介面可以直接寫一般的 Cron 以及 PHP Cron
PHP CURL 寫法
by Rain Chu | 3 月 10, 2023 | PHP , woocommerce , wordpress , 程式
在台灣的消費習慣就是需要極致的快和簡單,所以我們就是要有能在 WordPress 的 WooCommerce 中一鍵結帳的功能,這功能其實有很多外掛可以達成,但為了一個按鈕加了一個外掛,總覺得用牛刀在殺雞,還是直接在 functions.php 中加入程式碼比較快,能改的東西也比較多。
woocommerce 商品 一鍵購物 修改 functions.php 找到佈景主題下的 functions.php 在後面加入以下的程式碼
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles', 15 );
function addOneClickCheckoutBtn() {
// 取得商品ID
$current_product_id = get_the_ID();
// 依據商品ID取得商品
$product = wc_get_product( $current_product_id );
// 取得結帳網址
$checkout_url = WC()->cart->get_checkout_url();
// 只在簡單商品中運行
if( $product->is_type( 'simple' ) ){
?>
<script>
jQuery(function($) {
$(".custom-checkout-btn").on("click", function()
{
$(this).attr("href", function()
{
return this.href + '&quantity=' + $('input.qty').val();
});
});?>
});
</script>
<style>
.raiseup_one_click_checkout_btn {
margin: 0px 1em !important;
}
</style>
<?php
echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="single_add_to_cart_button button raiseup_one_click_checkout_btn">直接結帳</a>';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'addOneClickCheckoutBtn' ); 修改位置、顏色 CSS 已符合你的商品頁面 程式碼中,有新增一個 CSS 的類別 raiseup_one_click_checkout_btn ,可以透過以下的程式碼去修正按鈕的顏色、字型大小
<style>
.raiseup_one_click_checkout_btn {
margin: 0px 1em !important;
}
</style>
近期留言