Table of Contents

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:

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:

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:

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