User Tools

Site Tools


Sidebar

Databases

k22:k22.10:start

22.10.0 Component Memcached (gb.memcached)

Many will ask themselves: What is Memcached? On the website https://memcached.org you will find a good answer:

  • Memcached is a “…free and open source, high performance distributed memory object caching system.
  • It is generic, but intended for use in accelerating dynamic web applications by reducing database load.
  • Memcached is an in-memory key-value store for small pieces of arbitrary data (strings, objects) from database call results, API calls or page rendering. Memcached is simple yet powerful”.

The gb.memcached component with its single Memcached class aims to provide you with a fully usable memory interface.

22.10.0.1 Properties of the Memcached class

The Memcached class has these properties

PropertyData typeDescription
DebugBooleanIndicates whether the debugging mode is switched on or switches on the debugging mode with the value True.
ErrorStringReturns the error string returned by the last command that failed.
HostStringReturns or sets the host from the memcached server (MC server). The default host is “localhost” or “127.0.0.1”.
PortIntegerReturns or sets the port that is used for the connection from the MC client to the MC server.
SlabsAutomoveIntegerActivates or deactivates the “SlabsAutomove” function, which prevents defragmentation in the cache memory when activated (1). The default value is 1.
StatusIntegerReturns the status of the connection from the MC client to the MC server.
VersionStringReturns the current version of the Memcached server.

Table 22.10.0.1.1 : Properties of the Memcached class

This allows you to create a new Memcached client object, then set selected properties and finally start the configured client:

    Dim hMCClient As Memcached
 
    hMCClient = New Memcached 	        '-- A new MC client(!) is created
    hMCClient.Host = "localhost"      	'-- Default-Host: localhost; Alternative: "127.0.0.1"
    hMCClient.Port = 11211            	'-- Default-Port-Number
    hMCClient.Debug = True            	'-- True → Only for tests in the IDE
 
    If Not hMCClient.Open() Then
       Message.Error(("Memcached.Open-Error") & gb.NewLine & Error.Text & gb.NewLine & Error.Where)
       Return
    Endif

Notes:

  • If the Memcached property Debug is set to True, a Memcached client issues all commands sent to the MC server in a console.
  • The Memcached status can alternatively assume the following values: Net.Inactive (0) → The connection from the MC client to the MC server is closed. Net.Connected (7) → The MC client is connected to the MC server.
  • The default port number for the Memcached server is 11211.

22.10.0.2 Methods of the Memcached class

The Memcached class has these five methods:

MethodReturn typeDescription
Open()BooleanStarts a connection from the MC client to the MC server and returns True if the opening was successful.
Close()BooleanCloses a connection from the MC client to the MC server and returns True if the close was successful.
Retrieve ( sKey As Variant [ , iCas As Integer ] )CollectionThe method is used to retrieve a value from the MC-Server. sKey can be a single key or an array of keys. Set the optional parameter 'iCas' if you want to know whether the object has been changed since it was last retrieved by the calling client. The function returns a list (data type: collection) of key-value pairs as the function value.
Flush ( [ iDelay As Integer ] )-The command deletes all key-value pairs on the MC server. An optional parameter iDelay is accepted, which specifies the time after which the key-value pairs are actually deleted.
Exec ( sCommand As String [ , vData As Variant, bRaw As Boolean ] )VariantThe function provides direct access to the MC-Server. The command used is specified with 'sCommand'. The first optional parameter 'vData' represents the data to be saved. The second optional parameter specifies whether the data is available in the original.

Table 22.10.0.2.1 : Methods of the Memcached class

22.10.0.3 Constants of the Memcached class

The following constants relate to the output of the Memcached.Exec(…) method, among other things!

ConstantValueDescription
Stored0Indicates that a key-value pair with the specified key was successfully stored.
NotStored1Indicates that a key-value pair with the specified key could not be stored.
Exists2Indicates that a key-value pair with the specified key that you want to save with a Cas command has been changed since the last retrieval.
NotFound3Indicates that a key-value pair with the specified key was not found.
Deleted4Indicates that a key-value pair with the specified key was successfully deleted.
Touched5Indicates that the expiry time for a key-value pair with the specified key has been updated.

Table 22.10.0.3.1 : Constants of the Memcached class

22.10.0.4 Virtual class _Memcached_Key (gb.memcached)

You can use the Memcached class like a read-only array Memcached[ ]:

  Dim hMemcached As Memcached
  Dim hMemcachedKey As _Memcached_Key
  hMemcachedKey = hMemcached [ sKey As String ]

A virtual Memcached key object linked to the key 'sKey' (data type String) is returned. This is the preferred method for read and write data access.

The _Memcached_Key class has these two properties:

PropertyData typeDescription
ValueVariantReturns the value for the specified key or sets the value.
CasIDIntegerProvides an ID for a specific key.

Table 22.10.0.4.1 : Properties of the _Memcached_Key class

If you save a new key-value pair that does not yet exist on the MC server, for example, hMCClient[sCurKey].CasID returns the value 0.

The _Memcached_Key class has these five methods:

MethodReturn typeDescription
Cas ( sData As String )BooleanReturns True if the change to the key-value pair for the key used was successful.
DeleteBooleanReturns True if the deletion of the key-value pair for the key used was successful.
Decrement ( [ iStep As Integer ] )IntegerReturns the value of the counter for the key used reduced by the value of the iStep parameter.
Increment ( [ iStep As Integer ] )IntegerReturns the value of the counter for the key used increased by the value of the iStep parameter.
Touch ( iExpire As Integer )BooleanReturns True if setting the expiration time for the value with the key used was successful.

Table 22.10.0.4.2 : Methods of the _Memcached_Key class

Notes:

  • The smallest function value of the Decrement ( [ iStep As Integer ] ) method is 0. All further calls to the function then also return 0.
  • The iExpire parameter of the Touch method ( iExpire As Integer ) defines the expiry time of the data in seconds. The default value is 0 and means: The value never expires. Regardless of this, the value can be deleted from the cache at any time to make room for other key-value pairs!

You should know this before using the gb.memcached component in practice:

  • There is no access control: any MC client connected to an MC server can read all stored data on the MC server and can also write data.
  • All data is only stored in the memory. When a programme that uses the gb.memcached component is terminated, the data is no longer available.
  • You can only access the data with a unique key. Iterating is not possible!
  • Data can be saved either permanently or for a limited time during the runtime of the memcached client. In the latter case, the Memcached server automatically deletes the data.
  • The data type for a key is string.
  • The data type for a value is either Variant or Object.
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k22/k22.10/start.txt · Last modified: 08.05.2024 by emma

Page Tools