Skip to content

Commit 85ba512

Browse files
author
Teddy Grenman
committed
Add isPristine method.
1 parent 682de86 commit 85ba512

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

memcached-api.php

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public function getResultMessage( ) {}
149149

150150
public function isPersistent( ) {}
151151

152+
public function isPristine( ) {}
153+
152154
}
153155

154156
class MemcachedException extends Exception {

php_memcached.c

+24
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ typedef struct {
178178
} *obj;
179179

180180
bool is_persistent;
181+
bool is_pristine;
181182
int rescode;
182183
int memc_errno;
183184
} php_memc_t;
@@ -282,6 +283,7 @@ static PHP_METHOD(Memcached, __construct)
282283
}
283284

284285
i_obj = (php_memc_t *) zend_object_store_get_object(object TSRMLS_CC);
286+
i_obj->is_pristine = false;
285287

286288
if (persistent_id) {
287289
zend_rsrc_list_entry *le = NULL;
@@ -317,6 +319,7 @@ static PHP_METHOD(Memcached, __construct)
317319

318320
m_obj->serializer = MEMC_G(serializer);
319321
m_obj->compression = true;
322+
i_obj->is_pristine = true;
320323

321324
if (is_persistent) {
322325
zend_rsrc_list_entry le;
@@ -2015,6 +2018,23 @@ static PHP_METHOD(Memcached, isPersistent)
20152018

20162019
RETURN_BOOL(i_obj->is_persistent);
20172020
}
2021+
/* }}} */
2022+
2023+
/* {{{ Memcached::isPristine()
2024+
Returns the true if instance is recently created */
2025+
static PHP_METHOD(Memcached, isPristine)
2026+
{
2027+
MEMC_METHOD_INIT_VARS;
2028+
2029+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
2030+
return;
2031+
}
2032+
2033+
MEMC_METHOD_FETCH_OBJECT;
2034+
2035+
RETURN_BOOL(i_obj->is_pristine);
2036+
}
2037+
/* }}} */
20182038

20192039
/****************************************
20202040
Internal support code
@@ -2947,6 +2967,9 @@ ZEND_END_ARG_INFO()
29472967

29482968
ZEND_BEGIN_ARG_INFO(arginfo_isPersistent, 0)
29492969
ZEND_END_ARG_INFO()
2970+
2971+
ZEND_BEGIN_ARG_INFO(arginfo_isPristine, 0)
2972+
ZEND_END_ARG_INFO()
29502973
/* }}} */
29512974

29522975
/* {{{ memcached_class_methods */
@@ -3002,6 +3025,7 @@ static zend_function_entry memcached_class_methods[] = {
30023025
MEMC_ME(setOptions, arginfo_setOptions)
30033026

30043027
MEMC_ME(isPersistent, arginfo_isPersistent)
3028+
MEMC_ME(isPristine, arginfo_isPristine)
30053029
{ NULL, NULL, NULL }
30063030
};
30073031
#undef MEMC_ME

tests/check_if_pristine.phpt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Check if persistent object is new or an old persistent one
3+
--SKIPIF--
4+
<?php if (!extension_loaded("memcached")) print "skip";
5+
?>
6+
--FILE--
7+
<?php
8+
$m1 = new Memcached('id1');
9+
$m1->setOption(Memcached::OPT_PREFIX_KEY, "foo_");
10+
11+
var_dump($m1->isPristine());
12+
13+
$m1 = new Memcached('id1');
14+
var_dump($m1->isPristine());
15+
16+
$m2 = new Memcached('id1');
17+
var_dump($m2->isPristine());
18+
// this change affects $m1
19+
$m2->setOption(Memcached::OPT_PREFIX_KEY, "bar_");
20+
21+
$m3 = new Memcached('id2');
22+
var_dump($m3->isPristine());
23+
24+
$m3 = new Memcached();
25+
var_dump($m3->isPristine());
26+
--EXPECT--
27+
bool(true)
28+
bool(false)
29+
bool(false)
30+
bool(true)
31+
bool(true)

0 commit comments

Comments
 (0)