Memcached (or the Memcache Daemon) is a general-purpose networked memory
caching system that was originally developed by Danga Interactive for
LiveJournal, but is now used by many other sites. It is often used to speed up
dynamic database-driven websites by caching data and objects in RAM to reduce the
number of times an external data source (such as a database or API) must be read.
Memcached has been used by web sites including YouTube, Facebook and Twitter.
Values are store as keys (names or labels) and the associated value.
SparForte supports memcached natively (that is, using pragmas you can import
and export variable to/from Memcached.) The memcached support is built on the
PegaSoft PegaSock socket library, which supports local memcached caching as well
as distributed, redundant memcached caching clusters.
The Memcache package is a binding to the PegaSock library, giving a more
flexible interface to memcache than the pragmas provide. For example, you
can use any keys you want, rather than using the variable name as the key.
The names of the procedures and functions are equivalent to the commands
of the memcached protocol: you can find out more information on them by
consulting the memcached documentation.
The following is a trivial example with one memcached server running
on localhost port 11211.
=> c := memcache.new_cluster
=> (Assuming c is a new memcache.memcache_cluster variable)
=> memcache.register_server( c, "localhost", 11211 )
=> key : string := "candy"
=> memcache.set( c, key, "cane" )
=> ? memcache.get( c, key )
cane
=>
Example: Setting key "candy" to value "cane" and retrieving it again
Normally a memcache cluster would have two or more servers. The key
would automatically be stored on two servers. If a server in the cluster
fails, a program can still retrieve the data.
PegaSock implements an escallating backoff mechanism: if a server cannot
be accessed, it is marked as down. After a suitable period, PegaSock will
attempt to reconnect. If both servers containing the data are down, the next
few memcache calls automatically fail. If the servers are still down, it
backs off for a longer period of time. This is done to prevent a cascading
failure.
Some functionality (such as cas) is not implemented.
Because memcached has no security features, running in a restricted
shell will disable the memcache package. The package can also be
disabled with pragma restriction.
memcache.add( cl, k, v )
Store value v for key k in memcache cluster cl if the key does not exist.
Otherwise, an error is displayed but execution continues.
Look up the value previously stored for the key k in memcache cluster cl
and return it. If the key does not exist, return an empty string. If there
are two or more servers in the cluster, get will alternate between the servers
with the key in order to distribute load.
An exception is thrown if no servers are defined in the cluster
See Also
-
Compare With
Ada: PegaSock.Memcache.Get PHP: memcached::get
cl := memcache.new_cluster
Allocate a new memcache cluster record. The record is used to identify
servers and manage connections.
Example
mc_cluster := memcache.new_cluster;
Parameters
Param
Mode
Type
Default
Description
cl
return value
memcache.memcache_cluster
required
the new memcache cluster
Exceptions
An exception is thrown if no servers are defined in the cluster
Restrictions
Not allowed in a restricted shell or with no_memcache restriction
See Also
-
Compare With
Ada: N/A PHP: new memcached
Implementation Notes
Because the memcache_cluster record is private, SparForte allocates
clusters internally using a linked list. The records are not freed until
the script execution end.
memcache.prepend( cl, k, v )
Concatenate value v to the head of the current value for key k in
memcache cluster cl.
Example
memcache.set( mc_cluster, "company_name", "PegaSoft" ); memcache.prepend( mc_cluster, "company_name", "This is " ); -- company_name is "This is PegaSoft"
Parameters
Param
Mode
Type
Default
Description
cl
in out
memcache.memcache_cluster
required
the memcache cluster to use
k
in
any string type
required
the name to identify the value
v
in
any string type
required
the value to prepend
Exceptions
An exception is thrown if no servers are defined in the cluster
Restrictions
Not allowed in a restricted shell or with no_memcache restriction
Identify a new server to use in the memcache cluster. The server is
identified by hostname h and TCP/IP port p. The servers should always be
registered in the same order for this cluster. No network connection is
opened: network connections are opened when data is read or written. Do not
register the same server twice.
Overwrite the current value of key k memcache cluster cl with value v, if
a value already exists. If the key doesn't exist, a warning is displayed but
execution continues.
Return detailed memached statistics for all servers in the cluster. The
first item in the row is the server number. For the meaning of the stats,
consult the memcached documentation.
Example
? memcache.stats( mc_cluster );-- display all stats
Parameters
Param
Mode
Type
Default
Description
cl
in out
memcache.memcache_cluster
required
the memcache cluster to use
s
return value
string
required
the memcached stats
Exceptions
An exception is thrown if no servers are defined in the cluster
Restrictions
Not allowed in a restricted shell or with no_memcache restriction
Chooses a server from the cluster at random and display the version
of memcached that is running on the server. (It would be advisable to run
the same version on all servers.)
Example
? memcache.version( mc_cluster ); -- displays the version e.g. 1.4.5
Parameters
Param
Mode
Type
Default
Description
cl
in out
memcache.memcache_cluster
required
the memcache cluster to use
s
return value
string
required
the memcached version
Exceptions
An exception is thrown if no servers are defined in the cluster
Restrictions
Not allowed in a restricted shell or with no_memcache restriction