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.
The Memcached class has these properties
Property | Data type | Description |
---|---|---|
Debug | Boolean | Indicates whether the debugging mode is switched on or switches on the debugging mode with the value True. |
Error | String | Returns the error string returned by the last command that failed. |
Host | String | Returns or sets the host from the memcached server (MC server). The default host is “localhost” or “127.0.0.1”. |
Port | Integer | Returns or sets the port that is used for the connection from the MC client to the MC server. |
SlabsAutomove | Integer | Activates or deactivates the “SlabsAutomove” function, which prevents defragmentation in the cache memory when activated (1). The default value is 1. |
Status | Integer | Returns the status of the connection from the MC client to the MC server. |
Version | String | Returns 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:
The Memcached class has these five methods:
Method | Return type | Description |
---|---|---|
Open() | Boolean | Starts a connection from the MC client to the MC server and returns True if the opening was successful. |
Close() | Boolean | Closes 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 ] ) | Collection | The 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 ] ) | Variant | The 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
The following constants relate to the output of the Memcached.Exec(…) method, among other things!
Constant | Value | Description |
---|---|---|
Stored | 0 | Indicates that a key-value pair with the specified key was successfully stored. |
NotStored | 1 | Indicates that a key-value pair with the specified key could not be stored. |
Exists | 2 | Indicates 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. |
NotFound | 3 | Indicates that a key-value pair with the specified key was not found. |
Deleted | 4 | Indicates that a key-value pair with the specified key was successfully deleted. |
Touched | 5 | Indicates 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
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:
Property | Data type | Description |
---|---|---|
Value | Variant | Returns the value for the specified key or sets the value. |
CasID | Integer | Provides 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:
Method | Return type | Description |
---|---|---|
Cas ( sData As String ) | Boolean | Returns True if the change to the key-value pair for the key used was successful. |
Delete | Boolean | Returns True if the deletion of the key-value pair for the key used was successful. |
Decrement ( [ iStep As Integer ] ) | Integer | Returns the value of the counter for the key used reduced by the value of the iStep parameter. |
Increment ( [ iStep As Integer ] ) | Integer | Returns the value of the counter for the key used increased by the value of the iStep parameter. |
Touch ( iExpire As Integer ) | Boolean | Returns 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: