重构缓存策略中的部分函数。
This commit is contained in:
@@ -68,6 +68,7 @@ struct cache_param
|
|||||||
|
|
||||||
char no_revalidate;
|
char no_revalidate;
|
||||||
char cache_dyn_url;
|
char cache_dyn_url;
|
||||||
|
char cache_html;
|
||||||
char cache_cookied_cont;
|
char cache_cookied_cont;
|
||||||
char ignore_req_nocache;
|
char ignore_req_nocache;
|
||||||
char ignore_res_nocache;
|
char ignore_res_nocache;
|
||||||
@@ -388,30 +389,19 @@ char * cookie_scanvalue(const char * key, const char * cookies, char * val, size
|
|||||||
FREE(&cookie_dup);
|
FREE(&cookie_dup);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
char* url_remove_qs(const char* url, int qs_num, char* ignore_qs[])
|
||||||
|
|
||||||
char* get_cache_key(const struct tfe_http_half * request, const struct cache_key_descr* desc)
|
|
||||||
{
|
{
|
||||||
if(desc==NULL|| !desc->is_not_empty)
|
char* url_copy=tfe_strdup(url);
|
||||||
{
|
size_t target_size= strlen(url_copy)+1;
|
||||||
return NULL;
|
char* target_url=ALLOC(char, target_size);
|
||||||
}
|
|
||||||
int i=0, shall_ignore=0;
|
int i=0, shall_ignore=0;
|
||||||
|
|
||||||
char *token=NULL,*sub_token=NULL,*saveptr;
|
char *token=NULL,*sub_token=NULL,*saveptr;
|
||||||
char* url=tfe_strdup(request->req_spec.url);
|
|
||||||
const char* cookie=NULL;
|
|
||||||
char cookie_val[256]={0}; //most 256 bytes for cookie key
|
|
||||||
size_t key_size=strlen(url)+sizeof(cookie_val);
|
|
||||||
char* cache_key=ALLOC(char, key_size);
|
|
||||||
char* query_string=NULL;
|
|
||||||
|
|
||||||
if(desc->qs_num>0)
|
char* query_string=NULL;
|
||||||
{
|
query_string=strchr(url_copy, '?');
|
||||||
query_string=strchr(url, '?');
|
|
||||||
if(query_string!=NULL)
|
if(query_string!=NULL)
|
||||||
{
|
{
|
||||||
strncat(cache_key, url, MIN(query_string-url,key_size));
|
strncat(target_url, url_copy, MIN(query_string-url_copy,target_size));
|
||||||
query_string++;
|
query_string++;
|
||||||
for (token = query_string; ; token= NULL)
|
for (token = query_string; ; token= NULL)
|
||||||
{
|
{
|
||||||
@@ -419,9 +409,9 @@ char* get_cache_key(const struct tfe_http_half * request, const struct cache_key
|
|||||||
if (sub_token == NULL)
|
if (sub_token == NULL)
|
||||||
break;
|
break;
|
||||||
shall_ignore=0;
|
shall_ignore=0;
|
||||||
for(i=0; i<desc->qs_num; i++)
|
for(i=0; i<qs_num; i++)
|
||||||
{
|
{
|
||||||
if(0==strncasecmp(sub_token, desc->ignore_qs[i], strlen(desc->ignore_qs[i])))
|
if(0==strncasecmp(sub_token, ignore_qs[i], strlen(ignore_qs[i])))
|
||||||
{
|
{
|
||||||
shall_ignore=1;
|
shall_ignore=1;
|
||||||
break;
|
break;
|
||||||
@@ -429,28 +419,51 @@ char* get_cache_key(const struct tfe_http_half * request, const struct cache_key
|
|||||||
}
|
}
|
||||||
if(!shall_ignore)
|
if(!shall_ignore)
|
||||||
{
|
{
|
||||||
strncat(cache_key, sub_token, key_size);
|
strncat(target_url, sub_token, target_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
strncat(cache_key, url, key_size);
|
strncat(target_url, url_copy, target_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FREE(&(url_copy));
|
||||||
|
return target_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* get_cache_key(const struct tfe_http_half * request, const struct cache_key_descr* desc)
|
||||||
|
{
|
||||||
|
if(desc==NULL|| !desc->is_not_empty)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
char* url_no_qs=NULL;
|
||||||
|
const char* cookie=NULL;
|
||||||
|
char cookie_val[256]={0}; //most 256 bytes for cookie key
|
||||||
|
|
||||||
|
size_t key_size=strlen(request->req_spec.url)+sizeof(cookie_val);
|
||||||
|
char* cache_key=ALLOC(char, key_size);
|
||||||
|
|
||||||
|
if(desc->qs_num>0)
|
||||||
|
{
|
||||||
|
url_no_qs=url_remove_qs(request->req_spec.url, desc->qs_num, desc->ignore_qs);
|
||||||
|
strncat(cache_key, url_no_qs, key_size);
|
||||||
|
FREE(&url_no_qs);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
strncat(cache_key, url, key_size);
|
strncat(cache_key, request->req_spec.url, key_size);
|
||||||
}
|
}
|
||||||
if(desc->include_cookie && (cookie=tfe_http_std_field_read(request, TFE_HTTP_COOKIE))!=NULL)
|
if(desc->include_cookie && (cookie=tfe_http_std_field_read(request, TFE_HTTP_COOKIE))!=NULL)
|
||||||
{
|
{
|
||||||
cookie_scanvalue(desc->include_cookie, cookie, cookie_val, sizeof(cookie_val));
|
cookie_scanvalue(desc->include_cookie, cookie, cookie_val, sizeof(cookie_val));
|
||||||
if(strlen(cookie_val)>0)
|
if(strlen(cookie_val)>0)
|
||||||
{
|
{
|
||||||
|
strncat(cache_key, "/C/", key_size);
|
||||||
strncat(cache_key, cookie_val, key_size);
|
strncat(cache_key, cookie_val, key_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FREE(&(url));
|
|
||||||
return cache_key;
|
return cache_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -917,6 +930,7 @@ struct cache_mid
|
|||||||
char shall_bypass;
|
char shall_bypass;
|
||||||
char is_using_exception_param;
|
char is_using_exception_param;
|
||||||
char is_dyn_url;
|
char is_dyn_url;
|
||||||
|
char is_html;
|
||||||
char has_cookie;
|
char has_cookie;
|
||||||
char use_cnt;
|
char use_cnt;
|
||||||
int cfg_id;
|
int cfg_id;
|
||||||
@@ -976,7 +990,6 @@ enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, u
|
|||||||
if(ret>0)
|
if(ret>0)
|
||||||
{
|
{
|
||||||
|
|
||||||
TFE_LOG_DEBUG(handle->logger, "cache policy %d matched: %s", cache_policy.config_id, request->req_spec.url);
|
|
||||||
ex_data=Maat_rule_get_ex_data(handle->ref_feather, &cache_policy, handle->cache_param_idx);
|
ex_data=Maat_rule_get_ex_data(handle->ref_feather, &cache_policy, handle->cache_param_idx);
|
||||||
if(ex_data!=NULL)
|
if(ex_data!=NULL)
|
||||||
{
|
{
|
||||||
@@ -989,6 +1002,15 @@ enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, u
|
|||||||
_mid->shall_bypass=1;
|
_mid->shall_bypass=1;
|
||||||
}
|
}
|
||||||
_mid->cfg_id=cache_policy.config_id;
|
_mid->cfg_id=cache_policy.config_id;
|
||||||
|
if(param->key_descr.is_not_empty)
|
||||||
|
{
|
||||||
|
_mid->cache_key=get_cache_key(request, &(param->key_descr));
|
||||||
|
}
|
||||||
|
TFE_LOG_DEBUG(handle->logger, "cache policy %d matched: url=%s alternative key=%s",
|
||||||
|
cache_policy.config_id,
|
||||||
|
request->req_spec.url,
|
||||||
|
_mid->cache_key!=NULL?_mid->cache_key:"null");
|
||||||
|
|
||||||
}
|
}
|
||||||
if(_mid->shall_bypass ||
|
if(_mid->shall_bypass ||
|
||||||
(!param->force_caching && !param->cache_dyn_url && _mid->is_dyn_url && param->key_descr.qs_num==0) ||
|
(!param->force_caching && !param->cache_dyn_url && _mid->is_dyn_url && param->key_descr.qs_num==0) ||
|
||||||
@@ -1047,15 +1069,7 @@ enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, u
|
|||||||
|
|
||||||
struct tango_cache_meta_get meta;
|
struct tango_cache_meta_get meta;
|
||||||
memset(&meta, 0, sizeof(meta));
|
memset(&meta, 0, sizeof(meta));
|
||||||
if(param->key_descr.is_not_empty)
|
meta.url=_mid->cache_key!=NULL?_mid->cache_key:request->req_spec.url;
|
||||||
{
|
|
||||||
_mid->cache_key=get_cache_key(request, &(param->key_descr));
|
|
||||||
meta.url = _mid->cache_key;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
meta.url = request->req_spec.url;
|
|
||||||
}
|
|
||||||
meta.get = _mid->req_fresshness;
|
meta.get = _mid->req_fresshness;
|
||||||
|
|
||||||
struct promise* p=future_to_promise(f_revalidate);
|
struct promise* p=future_to_promise(f_revalidate);
|
||||||
@@ -1152,7 +1166,7 @@ struct cache_update_context* web_cache_update_start(struct cache_handle* handle,
|
|||||||
enum cache_pending_action put_action;
|
enum cache_pending_action put_action;
|
||||||
struct tango_cache_ctx *write_ctx=NULL;
|
struct tango_cache_ctx *write_ctx=NULL;
|
||||||
char cont_type_str[TFE_STRING_MAX]={0}, user_tag_str[TFE_STRING_MAX]={0};
|
char cont_type_str[TFE_STRING_MAX]={0}, user_tag_str[TFE_STRING_MAX]={0};
|
||||||
const char* value=NULL;
|
const char* content_type=NULL;
|
||||||
char *tmp=NULL;
|
char *tmp=NULL;
|
||||||
int i=0, is_undefined_obj=0;
|
int i=0, is_undefined_obj=0;
|
||||||
size_t content_len=0;
|
size_t content_len=0;
|
||||||
@@ -1171,7 +1185,11 @@ struct cache_update_context* web_cache_update_start(struct cache_handle* handle,
|
|||||||
{
|
{
|
||||||
sscanf(session->resp->resp_spec.content_length, "%lu", &content_len);
|
sscanf(session->resp->resp_spec.content_length, "%lu", &content_len);
|
||||||
}
|
}
|
||||||
|
content_type=tfe_http_std_field_read(session->resp, TFE_HTTP_CONT_TYPE);
|
||||||
|
if(content_type!=NULL&& NULL!=strcasestr(content_type, "text/html"))
|
||||||
|
{
|
||||||
|
_mid->is_html=1;
|
||||||
|
}
|
||||||
put_action=tfe_cache_put_pending(session->resp, &resp_freshness);
|
put_action=tfe_cache_put_pending(session->resp, &resp_freshness);
|
||||||
switch(put_action){
|
switch(put_action){
|
||||||
case FORBIDDEN:
|
case FORBIDDEN:
|
||||||
@@ -1187,7 +1205,8 @@ struct cache_update_context* web_cache_update_start(struct cache_handle* handle,
|
|||||||
case UNDEFINED:
|
case UNDEFINED:
|
||||||
if(_mid->shall_bypass
|
if(_mid->shall_bypass
|
||||||
|| content_len > param->max_cache_obj_size
|
|| content_len > param->max_cache_obj_size
|
||||||
|| (!param->cache_cookied_cont && _mid->has_cookie) )
|
|| (!param->cache_cookied_cont && _mid->has_cookie)
|
||||||
|
|| (!param->cache_html && _mid->is_html))
|
||||||
{
|
{
|
||||||
ATOMIC_INC(&(handle->stat_val[STAT_CACHE_UPLOAD_BYPASS]));
|
ATOMIC_INC(&(handle->stat_val[STAT_CACHE_UPLOAD_BYPASS]));
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <event2/event.h>
|
#include <event2/event.h>
|
||||||
#include <event2/buffer.h>
|
#include <event2/buffer.h>
|
||||||
#include <tfe_future.h>
|
#include <tfe_future.h>
|
||||||
|
#include <MESA/Maat_rule.h>
|
||||||
|
|
||||||
enum cache_query_status
|
enum cache_query_status
|
||||||
{
|
{
|
||||||
@@ -13,7 +14,9 @@ enum cache_query_status
|
|||||||
WEB_CACHE_HIT
|
WEB_CACHE_HIT
|
||||||
};
|
};
|
||||||
struct cache_handle;
|
struct cache_handle;
|
||||||
struct cache_handle* create_web_cache_handle(const char* profile_path, const char* section, struct event_base* gc_evbase, void *logger);
|
struct cache_handle* create_web_cache_handle(const char* profile_path, const char* section,
|
||||||
|
struct event_base* gc_evbase, Maat_feather_t feather, void *logger);
|
||||||
|
|
||||||
struct cached_meta
|
struct cached_meta
|
||||||
{
|
{
|
||||||
size_t content_length;
|
size_t content_length;
|
||||||
@@ -25,7 +28,8 @@ const struct cached_meta* cache_query_result_read_meta(future_result_t * result)
|
|||||||
size_t cache_query_result_get_data(future_result_t * result, const unsigned char** pp_data);
|
size_t cache_query_result_get_data(future_result_t * result, const unsigned char** pp_data);
|
||||||
|
|
||||||
int web_cache_async_query(struct cache_handle* handle, unsigned int thread_id,
|
int web_cache_async_query(struct cache_handle* handle, unsigned int thread_id,
|
||||||
const struct tfe_http_half * request, struct future* f);
|
const struct tfe_http_half * request, struct cache_mid** mid, struct future* f);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
enum cache_query_result_type
|
enum cache_query_result_type
|
||||||
@@ -48,11 +52,12 @@ enum cache_pending_result
|
|||||||
PENDING_RESULT_MISS
|
PENDING_RESULT_MISS
|
||||||
};
|
};
|
||||||
struct cache_mid;
|
struct cache_mid;
|
||||||
|
void cache_mid_clear(struct cache_mid **mid);
|
||||||
|
|
||||||
const struct cached_meta* cache_pending_result_read_meta(future_result_t * result);
|
const struct cached_meta* cache_pending_result_read_meta(future_result_t * result);
|
||||||
enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, unsigned int thread_id,
|
enum cache_pending_result web_cache_async_pending(struct cache_handle* handle, unsigned int thread_id,
|
||||||
const struct tfe_http_half * request, struct cache_mid **mid, struct future* f_revalidate);
|
const struct tfe_http_half * request, struct cache_mid **mid, struct future* f_revalidate);
|
||||||
void cache_mid_free(struct cache_mid **mid);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user