Skip to content

Commit 2312fb2

Browse files
committed
添加阅读注释
1 parent 3ba380c commit 2312fb2

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

php_memcached.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ uint64_t s_zval_to_uint64 (zval *cas)
630630
/****************************************
631631
Iterate over memcached results and mget
632632
****************************************/
633-
633+
// tangwei: get方法的底层 后的结果,对标志位的处理,压缩,反序列化都在合理处理
634634
static
635635
memcached_return php_memc_result_apply(php_memc_object_t *intern, php_memc_result_apply_fn result_apply_fn, zend_bool fetch_delay, void *context)
636636
{
@@ -679,6 +679,7 @@ memcached_return php_memc_result_apply(php_memc_object_t *intern, php_memc_resul
679679
s_uint64_to_zval(&zcas, cas);
680680

681681
key = zend_string_init (res_key, res_key_len, 0);
682+
//tangwei:这一句很关键
682683
retval = result_apply_fn(intern, key, &val, &zcas, flags, context);
683684

684685
zend_string_release(key);
@@ -700,6 +701,8 @@ memcached_return php_memc_result_apply(php_memc_object_t *intern, php_memc_resul
700701
return status;
701702
}
702703

704+
705+
// tangwei:get的底层实现
703706
static
704707
zend_bool php_memc_mget_apply(php_memc_object_t *intern, zend_string *server_key, php_memc_keys_t *keys,
705708
php_memc_result_apply_fn result_apply_fn, zend_bool with_cas, void *context)
@@ -727,6 +730,7 @@ zend_bool php_memc_mget_apply(php_memc_object_t *intern, zend_string *server_key
727730
if (server_key) {
728731
status = memcached_mget_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), keys->mkeys, keys->mkeys_len, keys->num_valid_keys);
729732
} else {
733+
// tangwei: get方法走这里
730734
status = memcached_mget(intern->memc, keys->mkeys, keys->mkeys_len, keys->num_valid_keys);
731735
}
732736

@@ -858,7 +862,7 @@ zend_bool s_invoke_cache_callback(zval *zobject, zend_fcall_info *fci, zend_fcal
858862
/****************************************
859863
Wrapper for setting from zval
860864
****************************************/
861-
865+
//tangwei:压缩逻辑
862866
static
863867
zend_bool s_compress_value (php_memc_compression_type compression_type, zend_string **payload_in, uint32_t *flags)
864868
{
@@ -912,7 +916,9 @@ zend_bool s_compress_value (php_memc_compression_type compression_type, zend_str
912916

913917
/* Replace the payload with the compressed copy */
914918
if (compress_status) {
919+
// tangei: 设置flag
915920
MEMC_VAL_SET_FLAG(*flags, MEMC_VAL_COMPRESSED | compression_type_flag);
921+
// tangwei: 这里就是头部4字节的源代码
916922
payload = zend_string_realloc(payload, compressed_size + sizeof(uint32_t), 0);
917923

918924
/* Copy the uin32_t at the beginning */
@@ -931,6 +937,7 @@ zend_bool s_compress_value (php_memc_compression_type compression_type, zend_str
931937
return 0;
932938
}
933939

940+
// tangwei: 序列化数据并且设置标记位
934941
static
935942
zend_bool s_serialize_value (php_memc_serializer_type serializer, zval *value, smart_str *buf, uint32_t *flags)
936943
{
@@ -1009,6 +1016,7 @@ zend_bool s_serialize_value (php_memc_serializer_type serializer, zval *value, s
10091016
return 1;
10101017
}
10111018

1019+
// tangwei: zval 转化为 playload
10121020
static
10131021
zend_string *s_zval_to_payload(php_memc_object_t *intern, zval *value, uint32_t *flags)
10141022
{
@@ -1061,6 +1069,7 @@ zend_string *s_zval_to_payload(php_memc_object_t *intern, zval *value, uint32_t
10611069
{
10621070
smart_str buffer = {0};
10631071

1072+
// 序列化数据,这里也会设置flag
10641073
if (!s_serialize_value (memc_user_data->serializer, value, &buffer, flags)) {
10651074
smart_str_free(&buffer);
10661075
return NULL;
@@ -1083,6 +1092,7 @@ zend_string *s_zval_to_payload(php_memc_object_t *intern, zval *value, uint32_t
10831092
*
10841093
* No need to check the return value because the payload is always valid.
10851094
*/
1095+
// 压缩数据,这里也会设置flag
10861096
(void)s_compress_value (memc_user_data->compression_type, &payload, flags);
10871097
}
10881098

@@ -1103,6 +1113,7 @@ zend_bool s_should_retry_write (php_memc_object_t *intern, memcached_return stat
11031113
return s_memcached_return_is_error (status, 1);
11041114
}
11051115

1116+
//TODO:set方法调用
11061117
static
11071118
zend_bool s_memc_write_zval (php_memc_object_t *intern, php_memc_write_op op, zend_string *server_key, zend_string *key, zval *value, time_t expiration)
11081119
{
@@ -1270,6 +1281,7 @@ static PHP_METHOD(Memcached, __construct)
12701281
memc_user_data->compression_enabled = 1;
12711282
memc_user_data->encoding_enabled = 0;
12721283
memc_user_data->store_retry_count = MEMC_G(store_retry_count);
1284+
// 重要:这里实际上将set_udf_flags设置为1111111111111的二进制形式
12731285
memc_user_data->set_udf_flags = -1;
12741286
memc_user_data->is_persistent = is_persistent;
12751287

@@ -1413,6 +1425,7 @@ typedef struct {
14131425
zval *return_value;
14141426
} php_memc_get_ctx_t;
14151427

1428+
// tangwei get方法底层使用
14161429
static
14171430
zend_bool s_get_apply_fn(php_memc_object_t *intern, zend_string *key, zval *value, zval *cas, uint32_t flags, void *in_context)
14181431
{
@@ -1433,6 +1446,7 @@ zend_bool s_get_apply_fn(php_memc_object_t *intern, zend_string *key, zval *valu
14331446
return 0; /* Stop after one */
14341447
}
14351448

1449+
// tangwei: get方法
14361450
static
14371451
void php_memc_get_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key)
14381452
{
@@ -1457,6 +1471,7 @@ void php_memc_get_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key)
14571471
Z_PARAM_LONG(get_flags)
14581472
ZEND_PARSE_PARAMETERS_END();
14591473
} else {
1474+
// tangwei: get方法走这里
14601475
/* "S|f!l" */
14611476
ZEND_PARSE_PARAMETERS_START(1, 3)
14621477
Z_PARAM_STR(key)
@@ -3566,6 +3581,7 @@ memcached_return s_server_cursor_version_cb(const memcached_st *ptr, php_memcach
35663581
}
35673582

35683583

3584+
// tangwei: get方法 解压的函数
35693585
static
35703586
zend_string *s_decompress_value (const char *payload, size_t payload_len, uint32_t flags)
35713587
{
@@ -3580,6 +3596,7 @@ zend_string *s_decompress_value (const char *payload, size_t payload_len, uint32
35803596
return NULL;
35813597
}
35823598

3599+
// tangwei: 重点 get 判断flag
35833600
is_fastlz = MEMC_VAL_HAS_FLAG(flags, MEMC_VAL_COMPRESSION_FASTLZ);
35843601
is_zlib = MEMC_VAL_HAS_FLAG(flags, MEMC_VAL_COMPRESSION_ZLIB);
35853602

@@ -3605,6 +3622,7 @@ zend_string *s_decompress_value (const char *payload, size_t payload_len, uint32
36053622
ZSTR_VAL(buffer)[stored_length] = '\0';
36063623

36073624
if (!decompress_status) {
3625+
// tangwei: 报错的地方
36083626
php_error_docref(NULL, E_WARNING, "could not decompress value");
36093627
zend_string_release (buffer);
36103628
return NULL;

0 commit comments

Comments
 (0)