forked from sugarcrm/shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadow_cache.c
106 lines (96 loc) · 2.59 KB
/
shadow_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (C) 2014, SugarCRM Inc.
*
* This product is licensed by SugarCRM under the Apache License, Version 2.0 (the "License").
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_shadow.h"
#include "shadow_cache.h"
/* if the cache is full, clean it */
void shadow_cache_check_full(TSRMLS_D)
{
if(zend_hash_num_elements(&SHADOW_G(cache)) >= SHADOW_G(cache_size)) {
shadow_cache_clean(TSRMLS_C);
}
}
/* set cache segment ID from template/instance pair */
void shadow_cache_set_id(const char *template, const char *instance TSRMLS_DC)
{
char *segname;
int namelen;
uint *psegment;
if(SHADOW_G(cache_size) == 0) {
return;
}
namelen = spprintf(&segname, 0, "0\x9%s\x9%s", template, instance);
if(zend_hash_find(&SHADOW_G(cache), segname, namelen+1, (void **)&psegment) != SUCCESS) {
uint segment;
shadow_cache_check_full(TSRMLS_C);
segment = zend_hash_num_elements(&SHADOW_G(cache))+2; /* 0 and 1 are reserved */
psegment = &segment;
zend_hash_update(&SHADOW_G(cache), segname, namelen+1, (void **)psegment, sizeof(uint), NULL);
SHADOW_G(segment_id) = *psegment;
}
efree(segname);
SHADOW_G(segment_id) = *psegment;
}
int shadow_cache_segmented_name(char **outname, const char *name TSRMLS_DC)
{
return spprintf(outname, 0, "%d\x9%s", SHADOW_G(segment_id), name);
}
int shadow_cache_get(const char *name, char **entry TSRMLS_DC)
{
char *segname;
int namelen;
char *centry;
if(SHADOW_G(cache_size) == 0) {
return FAILURE;
}
namelen = shadow_cache_segmented_name(&segname, name TSRMLS_CC);
if(zend_hash_find(&SHADOW_G(cache), segname, namelen+1, (void **)¢ry) == SUCCESS) {
if(centry[0]) {
*entry = estrdup(centry);
} else {
*entry = NULL;
}
efree(segname);
return SUCCESS;
}
efree(segname);
return FAILURE;
}
void shadow_cache_put(const char *name, const char *entry TSRMLS_DC)
{
char *segname;
int namelen;
if(SHADOW_G(cache_size) == 0) {
return;
}
/* will copy the string */
if(!entry) {
entry = "";
}
namelen = shadow_cache_segmented_name(&segname, name TSRMLS_CC);
zend_hash_update(&SHADOW_G(cache), segname, namelen+1, (void **)entry, strlen(entry)+1, NULL);
efree(segname);
}
void shadow_cache_remove(const char *name TSRMLS_DC)
{
char *segname;
int namelen;
if(SHADOW_G(cache_size) == 0) {
return;
}
namelen = shadow_cache_segmented_name(&segname, name TSRMLS_CC);
zend_hash_del(&SHADOW_G(cache), segname, namelen+1);
efree(segname);
}
void shadow_cache_clean(TSRMLS_C)
{
zend_hash_clean(&SHADOW_G(cache));
}