【Redis实战】:2、使用Redis构建Web应用

【GitHub】:示例代码

1、Redis构建Web应用

uuid 通用唯一识别码:配置  curl URL传输:配置  JSON for Modern C++:配置

#pragma once
#include <cstdio>
#include <iostream>
#include <list>
#include <string>
#include <tr1/unordered_map>
#include <set>
#include <thread>
#include <locale>         // std::wstring_convert
#include <codecvt>        // std::codecvt_utf8
#include <algorithm>

#include <unistd.h>
#include <curl/curl.h>

#include <sw/redis++/redis++.h>
#include <uuid/uuid.h>
#include <nlohmann/json.hpp>

using namespace sw::redis;

class chapter_02 
{
public:
    void run()
    {
        auto redis = Redis("tcp://123456@192.168.xxx.xxx:6379");

        try
        {
            //test_login_cookies(redis);
            //test_shoppping_cart_cookies(redis);
            //test_cache_rows(redis);
            //test_cache_request(redis);
        }
        catch (std::exception& err)
        {
            std::cout << err.what() << std::endl;
        }
    }

	// .... 四测试函数代码后述

    // 删排在20000名后的商品,同时更新浏览数,暂未用到
    class rescale_viewed
    {
    private:
        Redis redis_;
        bool quit_;

    public:
        rescale_viewed(std::string cnf) :
            redis_(Redis(cnf)),
            quit_(false)
        {}

        void quit()
        {
            quit_ = true;
        }

        void run()
        {
            while (!quit_)
            {
                redis_.zremrangebyrank("viewed:", 20000, -1);   // 删除排在20000之后的商品
                redis_.zinterstore("viewed:", "viewed:", 0.5);   // 将浏览次数降低为原来一半,顾及新流行商品
                sleep(300);     // 5分钟之后再执行
            }
        }
    };
};
2、登录和cookie缓存
void test_login_cookies(Redis& redis)
{
    std::cout << "\n----- testLoginCookies -----" << std::endl;
    
    uuid_t uuid;    // 通用唯一识别码
    char buf[36];
    uuid_generate_random(uuid);
    uuid_unparse(uuid, buf);
    std::string token = buf;

    update_token(redis, token, "username", "itemX");    // 更新令牌
    std::cout << "We just logged-in/updated token: " + token << std::endl;
    std::cout << "For user: 'username'" << std::endl;
    std::cout << std::endl;

    std::cout << "What username do we get when we look-up that token?" << std::endl;
    auto r = check_token(redis, token);     // 检查令牌,判断是否登录,返用户ID
    std::cout << r << std::endl;
    std::cout << std::endl;
    assert(r.length() != 0);

    std::cout << "Let's drop the maximum number of cookies to 0 to clean them out" << std::endl;
    std::cout << "We will start a thread to do the cleaning, while we stop it later" << std::endl;


    auto obj = clean_sessions("tcp://123456@192.168.xxx.xxx:6379", 0);     // 清理旧会话
    auto thread = std::thread(&clean_sessions::run, &obj);
    sleep(1);
    obj.quit();
    thread.join();
    if (thread.joinable())
    {
        throw std::runtime_error("The clean sessions thread is still alive?!?");
    }

    auto s = redis.hlen("login:");      // 查询所有登录用户数 [令牌与登录用户间的映射]
    std::cout <<  "The current number of sessions still available is: " + std::to_string(s) << std::endl;
    assert(s == 0);
}

std::string check_token(Redis& redis, std::string token)
{
    auto res = redis.hget("login:", token);
    if (res) return res.value();  // 检查令牌,判断是否登录,返用户ID

    return "";     
}

// 修改令牌,每秒可记录2w+商品,同时维护商品分值省去计算
void update_token(Redis& redis, std::string token, std::string user, std::string item)
{
    auto timestamp = time(NULL);     // 取当前时间戳
    redis.hset("login:", token, user);      // 维持令牌与登录用户间的映射
    redis.zadd("recent:", token, static_cast<double>(timestamp));   // 记录令牌最后一次出现时间
    if (item.length()) 
    {
        redis.zadd("viewed:" + token, item, static_cast<double>(timestamp));    // 记录用户浏览过的商品
        redis.zremrangebyrank("viewed:" + token, 0, -26);   // 修剪,保持最近浏览过的25个商品
        redis.zincrby("viewed:", -1, item); // 浏览最多的商品,分值最低
    }
}

// 默认保留1000w会话,超限最多删100,日访500w每秒产生58个会话,每秒可清理网络1w+令牌,本地6w+令牌
class clean_sessions
{
private:
    Redis redis_;
    long long limit_;
    bool quit_;

public:
    clean_sessions(std::string cnf, int limit = 10000000) :     
        redis_(Redis(cnf)),
        limit_(limit),
        quit_(false)
    {}

    void quit()
    {
        quit_ = true;
    }

    void run()
    {
        while (!quit_)
        {
            auto size = redis_.zcard("recent:");    //找出目前已有令牌数
            if (size <= limit_) // 令牌数未超限制
            {
                try
                {
                    
                    sleep(1);    // 休眠1s并之后重查
                }
                catch (std::exception& err)
                {
                    std::cout << err.what() << std::endl;
                    return;
                }
                continue;
            }

            auto end_index = std::min(size - limit_, (long long)100);
            std::vector<std::string> tokens;    // 获取需移除的令牌ID
            redis_.zrange("recent:", 0, end_index - 1, std::back_inserter(tokens));     // 递增, 越前面越久远

            std::vector<std::string> session_keys;
            for (auto token : tokens)
                session_keys.push_back("viewed:" + token);

            redis_.del(session_keys.begin(), session_keys.end());       // 移除用户最近浏览商品记录
            redis_.hdel("login:", tokens.begin(), tokens.end());      // 移除令牌与登录用户间的映射
            redis_.zrem("recent:", tokens.begin(), tokens.end());     // 移除令牌最后一次出现时间
        }
    }
};

在这里插入图片描述

3、Redis实现购物车
void test_shoppping_cart_cookies(Redis& redis)
{
    std::cout << "\n----- testShopppingCartCookies -----" << std::endl;

    uuid_t uuid;    // 通用唯一识别码
    char buf[37] = { 0 };
    uuid_generate_random(uuid);
    uuid_unparse(uuid, buf);
    std::string token = buf;

    std::cout << "We'll refresh our session..." << std::endl;
    update_token(redis, token, "username", "itemX");    // 更新令牌
    std::cout << "And add an item to the shopping cart" << std::endl;
    add_to_cart(redis, token, "itemY", 3);  // 将商品加入购物车
    std::unordered_map<std::string, std::string> r;
    redis.hgetall("cart:" + token, std::inserter(r, r.begin()));      // Hash, return key-vals
    std::cout << "Our shopping cart currently has:" << std::endl;
    for (auto& item : r) 
    {
        std::cout << "  " + item.first + ": " + item.second << std::endl;
    }
    std::cout << std::endl;

    assert(r.size() >= 1);

    std::cout << "Let's clean out our sessions and carts" << std::endl;

	// 清理旧会话,同时将旧会话对应用户的购物车也一并删除
    auto obj = clean_full_sessions("tcp://123456@192.168.xxx.xxx:6379", 0);    
    auto thread = std::thread(&clean_full_sessions::run, &obj);
    sleep(1);
    obj.quit();
    thread.join();
    if (thread.joinable())
    {
        throw std::runtime_error("The clean sessions thread is still alive?!?");
    }

    std::unordered_map<std::string, std::string> r2;
    redis.hgetall("cart:" + token, std::inserter(r2, r2.begin()));      // 查询当前登录用户的购物车商品
    std::cout << "Our shopping cart now contains:" << std::endl;
    for (auto& item : r2)
    {
        std::cout << "  " + item.first + ": " + item.second << std::endl;
    }
    assert(r2.size() == 0);
}


// 购物车信息存入redis, 减少请求体积,还对数据分类可计算,cart:令牌ID,商品
void add_to_cart(Redis& redis, std::string session, std::string item, int count)
{
    if (count <= 0) 
        redis.hdel("cart:" + session, item);    // 若订购商品数不大于0,则从购物车移除
    else 
        redis.hset("cart:" + session, item, std::to_string(count));     // 将商品加入购物车
}


class clean_full_sessions
{
private:
    Redis redis_;
    long long limit_;
    bool quit_;

public:
    clean_full_sessions(std::string cnf, int limit = 10000000) :
        redis_(Redis(cnf)),
        limit_(limit),
        quit_(false)
    {}

    void quit()
    {
        quit_ = true;
    }

    void run()
    {
        while (!quit_)
        {
            long size = redis_.zcard("recent:");    //找出目前已有令牌数
            if (size <= limit_) // 令牌数未超限制
            {
                try
                {
                    sleep(1);   // 休眠1s并之后重查
                }
                catch (std::exception& err)
                {
                    std::cout << err.what() << std::endl;
                    return;
                }
                continue;
            }

            auto end_index = std::min(size - limit_, (long long)100);
            std::vector<std::string> sessions;  // 获取需移除的令牌ID
            redis_.zrange("recent:", 0, end_index - 1, std::back_inserter(sessions));   // 递增, 越前面越久远

            std::vector<std::string> session_keys;
            for (auto sess : sessions)
            {
                session_keys.push_back("viewed:" + sess);
                session_keys.push_back("cart:" + sess);
            }

            redis_.del(session_keys.begin(), session_keys.end());           // 移除用户最近浏览商品记录 和 购物车
            redis_.hdel("login:", sessions.begin(), sessions.end());      // 移除令牌与登录用户间的映射
            redis_.zrem("recent:", sessions.begin(), sessions.end());     // 移除令牌最后一次出现时间
        }
    }
};

在这里插入图片描述

4、数据行缓存
void test_cache_rows(Redis& redis)
{
    std::cout << "\n----- testCacheRows -----" << std::endl;
    std::cout << "First, let's schedule caching of itemX every 5 seconds" << std::endl;
    schedule_row_cache(redis, "itemX", 5);
    std::cout << "Our schedule looks like:" << std::endl;

    std::unordered_map<std::string, double> res_with_score;
    redis.zrange("schedule:", 0, -1, std::inserter(res_with_score, res_with_score.end()));
    for (auto& item : res_with_score)
    {
        std::cout << "  " + item.first + ", " + std::to_string(item.second) << std::endl;
    }
    assert(res_with_score.size() != 0);

    std::cout << "We'll start a caching thread that will cache the data..." << std::endl;

    auto obj = cache_rows("tcp://123456@192.168.xxx.xxx:6379");    // 缓存某数据行
    auto thread = std::thread(&cache_rows::run, &obj);

    sleep(1);
    std::cout << "Our cached data looks like:" << std::endl;
    auto r = redis.get("inv:itemX");    // 显示某数据行缓存
    std::string rf = r ? r.value() : "";
    std::cout << rf << std::endl;
    assert(rf.length() != 0);
    std::cout << std::endl;

    std::cout << "We'll check again in 5 seconds..." << std::endl;
    sleep(5);
    std::cout << "Notice that the data has changed..." << std::endl;
    auto r2 = redis.get("inv:itemX");   // 显示某数据行缓存
    std::string r2f = r2 ? r2.value() : "";
    std::cout << r2f << std::endl;
    std::cout << std::endl;
    assert(r2f.length() != 0);
    assert(rf.compare(r2f) != 0);

    std::cout << "Let's force un-caching" << std::endl;
    schedule_row_cache(redis, "itemX", -1);     // 取消某数据行缓存
    sleep(1);
    auto r3 = redis.get("inv:itemX");
    std::string r3f = r3 ? "false" : "true";
    std::cout << "The cache was cleared? " + r3f << std::endl;
    assert(!r3f.compare("true"));

    obj.quit();
    thread.join();
    if (thread.joinable()) 
    {
        throw std::runtime_error("The database caching thread is still alive?!?");
    }
}


// 常变页面:持续运行,不定期更新缓存,形成重复进行调度的自动缓存机制,同时可控制频率
class cache_rows
{
private:
    Redis redis_;
    bool quit_;
    static inventory inventory_;

public:
    cache_rows(std::string cnf) :
        redis_(Redis(cnf)),
        quit_(false)
    {}

    void quit()
    {
        quit_ = true;
    }

    void run()
    {
        nlohmann::json j;
        while (!quit_)
        {
            std::unordered_map<std::string, double> res_with_score;
            redis_.zrange("schedule:", 0, 0, std::inserter(res_with_score, res_with_score.end()));  //  递增, 越前面越久远, 获取第一个调度元素和分值

            auto next = res_with_score.begin();
            auto now = time(NULL);
            if (next == res_with_score.end() || (*next).second > static_cast<double>(now))  // 分值指定的时间尚未来临,则先休眠50毫秒
            {
                try
                {
                    usleep(50000);
                }
                catch (std::exception& err)
                {
                    std::cout << err.what() << std::endl;
                    return;
                }
                continue;
            }

            auto row_id = (*next).first;
            double delay = redis_.zscore("delay:", row_id).value();
            if (delay <= 0) // 不再缓存此数据行, 同时移除, 只需延迟值设小于等于0即可
            {
                redis_.zrem("delay:", row_id);
                redis_.zrem("schedule:", row_id);
                redis_.del("inv:" + row_id);
                continue;
            }

            auto row = inventory_.get(row_id);  // 从数据库取出行数据,编码成json格式存redis里
            redis_.zadd("schedule:", row_id, static_cast<double>(now) + delay);  // 更新调度时间
            row.to_json(j);
            redis_.set("inv:" + row_id, j.dump());  // 设缓存值
        }
    }
}; 


class inventory
{
public:
    std::string id_;
    std::string data_;
    long time_;

private:
    inventory(std::string id)
    {
        id_ = id;
        data_ = "data to cache...";
        time_ = time(NULL);
    }

public:
    static inventory get(std::string id)
    {
        return inventory(id);
    }

    void to_json(nlohmann::json& j)
    {
        j = nlohmann::json{ {"id", id_}, {"data", data_}, {"time", time_} };
    }

    void from_json(const nlohmann::json& j)
    {
        j.at("id").get_to(id_);
        j.at("data").get_to(data_);
        j.at("time").get_to(time_);
    }
};


// 调度行缓存,设时限
void schedule_row_cache(Redis& redis, std::string row_id, int delay)
{
    redis.zadd("delay:", row_id, delay);    // 设置数据行的延迟值
    redis.zadd("schedule:", row_id, static_cast<double>(time(NULL)));    // 立即对需要缓存的数据行进行调度
}

在这里插入图片描述

5、网页缓存
void test_cache_request(Redis& redis)
{
    std::cout << "\n----- testCacheRequest -----" << std::endl;
    
    uuid_t uuid;    // 通用唯一识别码
    char buf[36];
    uuid_generate_random(uuid);
    uuid_unparse(uuid, buf);
    auto token = std::string(buf);

    auto cb = new request_call_back;    // 回调类

    update_token(redis, token, "username", "itemX");    // 更新令牌
    auto url = std::string("http://test.com/?item=itemX");
    std::cout << "We are going to cache a simple request against " + url << std::endl;
    auto result = cache_request(redis, url, cb);    // 网页缓存
    std::cout << "We got initial content:\n" + result << std::endl;
    std::cout << std::endl;

    assert(result.length() != 0);

    std::cout << "To test that we've cached the request, we'll pass a bad callback" << std::endl;
    auto result2 = cache_request(redis, url, NULL); // 网页缓存
    std::cout << "We ended up getting the same response!\n" + result2 << std::endl;

    assert(!result.compare(result2));

    assert(!can_cache(redis, "http://test.com/"));
    assert(!can_cache(redis, "http://test.com/?item=itemX&_=1234536"));
}

class call_back
{
public:
    virtual std::string call(std::string request)
    {
        return "";
    }
};

class request_call_back : public call_back
{
public:
    std::string call(std::string request) override
    {
        return "content for " + request;    //可对页面进行压缩
    }
};
    

// 不常变网页:5min内无需再生成视图,延迟含大量数据的页面,从20~50ms缩为1ms
std::string cache_request(Redis& redis, std::string request, call_back* db)
{
    if (!can_cache(redis, request))     // 不能缓存的请求,直接调回调
    {
        return db != NULL ? db->call(request) : "";
    }

    auto page_key = "cache:" + hash_request(request);   // 请求转换为一简单字符串键,便查找
    auto content = redis.get(page_key);     // 尝试查找被缓存的页面

    std::string res = "";
    if ((!content) && db != NULL)
    {
        res = db->call(request);    // 若页面还未缓存,则生成页面
        redis.setex(page_key, 300, res);    // 将新生成页面放入缓存,300s过期
    }
    else
    {
        res = content.value();
    }

    return res;
}



// 判断页面能否被缓存
bool can_cache(Redis& redis, std::string request)
{
    try 
    {
        auto report = req(request);     // 向某地址请求,返消息体
        std::unordered_map<std::string, std::string> params;
        if (report.length() != 0)
        {
            auto pms = util::split(report, "&");
            for (auto param : pms)
            {
                auto pos = param.find("=");
                if (pos != std::string::npos)
                {
                    auto first = param.substr(0, pos);
                    auto second = param.substr(pos + 1, param.length());
                    params[first] = second;
                }
                else
                    params[param] = "";
            }
        }

        auto item_id = extract_item_id(params);     // 尝试从页面里取出商品ID
        if (item_id.length() == 0 || is_dynamic(params))    // 检查页面是否被缓存 以及是否为商品页
            return false;
        
        auto rank = redis.zrank("viewed:", item_id);    // 取商品浏览次数排名
        return rank && (rank.value() < static_cast<long long>(10000));    // 根据排名,判断是否需要缓存
    }
    catch (std::exception& err)
    {
        return false;
    }
}

bool is_dynamic(std::unordered_map<std::string, std::string>& params)
{
    return params.find("_") != params.end();
}

std::string extract_item_id(std::unordered_map<std::string, std::string>& params)
{
    if(params.find("item") != params.end())
        return params["item"];

    return "";
}

std::string hash_request(std::string request)
{
    return std::to_string(util::hash_code(request));
}


std::string req(std::string request)
{
    CURL* curl;
    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, request.c_str());

    // ... 设置http头

    curl_easy_setopt(curl, CURLOPT_SSLVERSION, 1);

    //无证书
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

    //get请求
    curl_easy_setopt(curl, CURLOPT_POST, 0);


    std::string report_header = "";//回执回来的头数据
    std::string report = "";

    //设置CURLOPT_FOLLOWLOCATION为1,否则重定向后的数据不会返回
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

    //可看到调试信息
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    //接受服务器的ssl证书而不管合不合法 (相当于命令行中的--insecure)

    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _CURL_::write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &report_header);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _CURL_::write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &report);

    CURLcode code = curl_easy_perform(curl);
    if (code != CURLE_OK)
    {
        printf("curl_wasy_perform error = %s", curl_easy_strerror(code));
    }

    curl_easy_cleanup(curl);
    return report;
}

因无法向http://test.com/?item=itemX等地址发送请求,故注释can_cache以运行

在这里插入图片描述

6、字符串处理
namespace util
{
    // https://blog.csdn.net/hongxingabc/article/details/82846396
    std::wstring s2ws(const std::string& str)
    {
        using convert_typeX = std::codecvt_utf8<wchar_t>;
        std::wstring_convert<convert_typeX, wchar_t> converterX;

        return converterX.from_bytes(str);
    }

    std::string ws2s(const std::wstring& wstr)
    {
        using convert_typeX = std::codecvt_utf8<wchar_t>;
        std::wstring_convert<convert_typeX, wchar_t> converterX;

        return converterX.to_bytes(wstr);
    }

    int32_t hash_code(const std::string& str)
    {
        int32_t h = 0;                                 
        auto wstr = s2ws(str);                                   
        if (h == 0 && wstr.length() > 0) 
        {
            for (uint32_t i = 0; i < wstr.length(); i++) 
            {
                h = 31 * h + wstr.at(i);
            }
        }
        return h;
    }

    std::vector<std::string> split(const std::string& src, const std::string& pattern)
    {
        std::vector<std::string> res;
        if (src == "")
            return res;

        std::string strcom = src + pattern;
        auto pos = src.find(pattern);
        auto len = strcom.size();

        while (pos != std::string::npos)
        {
            auto costr = strcom.substr(0, pos);
            res.push_back(costr);

            strcom = strcom.substr(pos + pattern.size(), len);
            pos = strcom.find(pattern);
        }

        return res;
    }
}
7、请求数据处理
namespace _CURL_
{
    static size_t write_data(void* buf, size_t unit, size_t bufSize, std::string* data)
    {
        size_t size = unit * bufSize;
        char* tmp = (char*)malloc(size + 1);
        memcpy(tmp, buf, size);
        tmp[size] = '\0';
        data->append(tmp);
        free(tmp);
        return size;
    }

    class curl_intelligence
    {
    public:
        curl_intelligence()
        {
            curl_global_init(CURL_GLOBAL_ALL);
        }

        ~curl_intelligence()
        {
            curl_global_cleanup();
        }
    };
}

_CURL_::curl_intelligence g_curl;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值