Coder Social home page Coder Social logo

php-memoize's Introduction

Introduction

php-memoize is a PHP extension which transparently caches PHP functions, much like Perl's Memoize module.

It comes with the following storage modules which can be enabled at compile time:

  • memory: Simple per-request module with no dependencies. Since this is a per-request cache, neither TTLs specified in the memoize() call or memoize.default_ttl are used.
  • memcached: Uses libmemcached or the memcached PHP extension
  • apc: Uses the APC PHP extension

It also provides a simple interface (php_memoize_storage.h) if you want to define your own storage modules as PHP extensions.

Usage

<?php

function my_expensive_function($x) {
	sleep(10);
	return $x . 'bar';
}

memoize('my_expensive_function');

// now calls to my_expensive_function() are cached by their arguments

echo my_expensive_function('foo'); // returns "foobar" in 10s
echo my_expensive_function('foo'); // returns "foobar" in 0.0001s

N.B. Do not memoize functions which have side-effects or depend on anything other than their arguments, such as global variables or the current time.

Functions

bool memoize(mixed $callback [, int $ttl = 0 ])

Registers a function to be memoized. Like the callbacks taken for example by call_user_func(), $callback can be a string containing a function name or an array containing a class or object and a method name.

Unlike normal callbacks, it can refer to methods which aren't callable from this scope (e.g. private methods.) It can also refer to non-static methods as if they were static, so you don't need to have an instance available when you register the method.

The option $ttl argument specifies how long calls will be cached for (in seconds). If not provided or set to 0, the memoize.default_ttl ini setting is used.

Returns true if the function was successfully registered, or false and raises an E_WARNING error otherwise.

bool memoize_has_storage(string $module_name)

Returns whether the storage module with the given name is available.

bool memoize_memcached_set_connection(Memcached $m)

Sets an existing Memcached object to be be used for memoize storage, instead of creating a new connection using the servers defined in memoize.memcached.servers. This function is only available when the memcached extension is present, and only applicable when memoize.storage_module is set to "memcached".

Settings

All of the below ini settings can be changed at any time (PHP_INI_ALL).

General

memoize.storage_module String The storage module to use ("memory", "apc" or "memcached")
memoize.cache_namespace String A string to prepend to all cache keys, so that separate applications can use the same storage without conflicts.
memoize.default_ttl Integer The default number of seconds to store cache entries for (defaults to 3600, one hour.)

Memcached

memoize.memcached.servers String A [libmemcached configuration string](http://docs.libmemcached.org/libmemcached_configuration.html) (e.g. "--SERVER=127.0.0.1 --SERVER=10.0.2.2:22122"). This is only used if a connection hasn't been supplied by memoize_memcached_set_connection().

Installation

See Compiling shared PECL extensions with phpize for general instructions. The available configure options can be listed with configure --help.

php-memoize's People

Contributors

arraypad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-memoize's Issues

php_smart_str.h: No such file or directory

During the make command, I'm receiving the following fatal error:

/tmp/php-memoize/memoize.c:27:40: fatal error: ext/standard/php_smart_str.h: 
No such file or directory
 #include "ext/standard/php_smart_str.h"
                                        ^

I'm running the following version of PHP:

PHP 7.0.14 (cli) (built: Dec 10 2016 11:35:27) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.14, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans

Any suggestions?

ttl by function

Hi there,
Excellent work,
Why don't implement the traditional ttl by each function?
In my case there's a bunch of different ttl.
Also some write-off from memory something can be useful.
Thanks
Ricardo

Issues compiling under Win32

Hi

I'm running into issues compiling this under Win32, I pasted the compiler output below. Is compilation under windows supported?

I'm using VC9 under an xp with the usual Win32 compilation environment settings. I'm able to compile PHP and other extensions just fine (e.g. APC, memcache, ssh2, http, etc).

I tried bot "WITH" and "ENABLE" in config.w32 as well as compiling as shared and static. Same error ...

Any clues?

Thanks in advance.

"cl.exe" /D _USRDLL /D PHP5DLLTS_EXPORTS /D PHP_EXPORTS /D LIBZEND_EXPORTS /D TSRM_EXPORTS /D SAPI_EXPORTS /D WINVER=0x500 /D COMPILE_DL_BZ2 /D COMPILE_DL_COM_DOTNET /D COMPILE_DL_CURL /D COMPILE_DL_ENCHANT /D COMPILE_DL_FILEINFO /D COMPILE_DL_GD /D COMPILE_DL_GETTEXT /D COMPILE_DL_GMP /D COMPILE_DL_IMAP /D COMPILE_DL_INTERBASE /D COMPILE_DL_INTL /D COMPILE_DL_LDAP /D COMPILE_DL_MBSTRING /D COMPILE_DL_OPENSSL /D COMPILE_DL_PGSQL /D COMPILE_DL_SHMOP /D COMPILE_DL_SNMP /D COMPILE_DL_SOCKETS /D COMPILE_DL_SQLITE3 /D COMPILE_DL_SYBASE_CT /D COMPILE_DL_TIDY /D COMPILE_DL_APC /D COMPILE_DL_MYLCS /D COMPILE_DL_SSH2 /D COMPILE_DL_EXIF /D COMPILE_DL_MYSQL /D COMPILE_DL_MYSQLI /D COMPILE_DL_PDO_FIREBIRD /D COMPILE_DL_PDO_MYSQL /D COMPILE_DL_PDO_ODBC /D COMPILE_DL_PDO_PGSQL /D COMPILE_DL_PDO_SQLITE /D COMPILE_DL_SOAP /D COMPILE_DL_XMLRPC /D COMPILE_DL_XSL /D COMPILE_DL_HTTP /D COMPILE_DL_MEMCACHE  /nologo /FD /I . /I main /I Zend /I TSRM /I ext /D _WINDOWS /D ZEND_WIN32=1 /D PHP_WIN32=1 /D WIN32 /D _MBCS /W3   /wd4996 /D_USE_32BIT_TIME_T=1   /MP  /Zi /LD /MD /W3 /Ox /D NDebug /D NDEBUG /D ZEND_WIN32_FORCE_INLINE /GF /D ZEND_DEBUG=0 /D ZTS=1 /I "..\deps\include"  /D FD_SETSIZE=256 /FoRelease_TS\pecl\memoize\ /FdRelease_TS\pecl\memoize\ /FpRelease_TS\pecl\memoize\ /FRRelease_TS\pecl\memoize\ /c ..\pecl\memoize\memoize.c
memoize.c
..\pecl\memoize\memoize.c(82) : error C2059: syntax error : '}'
..\pecl\memoize\memoize.c(471) : error C2275: 'memoize_internal_function' : illegal use of this type as an expression
        c:\php-sdk\php53dev\vc9\x86\pecl\memoize\php_memoize.h(56) : see declaration of 'memoize_internal_function'
..\pecl\memoize\memoize.c(471) : error C2146: syntax error : missing ';' before identifier 'mem_func'
..\pecl\memoize\memoize.c(471) : error C2065: 'mem_func' : undeclared identifier
..\pecl\memoize\memoize.c(472) : error C2065: 'mem_func' : undeclared identifier
..\pecl\memoize\memoize.c(472) : error C2224: left of '.function' must have struct/union type
..\pecl\memoize\memoize.c(473) : error C2065: 'mem_func' : undeclared identifier
..\pecl\memoize\memoize.c(473) : error C2224: left of '.function_table' must have struct/union type
..\pecl\memoize\memoize.c(474) : error C2065: 'mem_func' : undeclared identifier

Segmentation fault

I'm running this in CentOS 5.7 64-bit and php 5.3.3. I've installed memoize and the memory storage engine. I'm getting a segfault. GDB backtrace included:

(gdb) run test.php
Starting program: /usr/bin/php test.php
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000
[Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault.
0x00002aaab096b61d in zif_memoize_call (ht=, return_value=, return_value_ptr=0x0, this_ptr=, 
    return_value_used=) at /root/php-memoize/memoize.c:317
317         if (call_user_function(&fe->common.scope->function_table, obj_pp, callable, *return_value_ptr, argc, (argc ? *args : NULL) TSRMLS_CC) == FAILURE) {
(gdb) bt
#0  0x00002aaab096b61d in zif_memoize_call (ht=, return_value=, return_value_ptr=0x0, this_ptr=, 
    return_value_used=) at /root/php-memoize/memoize.c:317
#1  0x00000000005e7789 in ?? ()
#2  0x00000000005bd84b in execute ()
#3  0x0000000000599df5 in zend_execute_scripts ()
#4  0x000000000054ace8 in php_execute_script ()
#5  0x0000000000622b7e in ?? ()
#6  0x00000039d061d994 in __libc_start_main () from /lib64/libc.so.6
#7  0x0000000000421789 in _start ()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.