Memcached installation and use
1. Install Memcached
-On Linux systems, use the following command to install Memcached:
sudo apt-get install memcached
-On Windows systems, you can download pre compiled binary files from the Memcached official website, then extract and run them.
2. Start Memcached
-On Linux systems, by default, Memcached will automatically start after installation and listen on the default port 11211.
-On Windows systems, locate the extracted Memcached folder and execute the 'memcached. exe' file to start.
3. Connect to Memcached
Memcached can be used through a network connection. In Python, the 'pymemcache' library can be used to connect and operate on Memcached.
Install the 'pymemcache' library:
python
pip install pymemcache
Connect to Memcached:
python
from pymemcache.client.base import Client
Client=Client ('localhost ', 11211) # Connect to the local Memcached, with the default port being 11211
4. Create a data table, insert, modify, query, and delete data
In Memcached, data is stored in a key value pair, which can use any string as the key, and the value can be any type of data.
Create a data table:
python
client.set('table:example:id1', {'name': 'John', 'age': 30, 'city': 'New York'})
client.set('table:example:id2', {'name': 'Alice', 'age': 25, 'city': 'London'})
Insert data:
python
client.set('table:example:id3', {'name': 'Bob', 'age': 35, 'city': 'Paris'})
Modify data:
python
client.set('table:example:id2', {'name': 'Alice Smith', 'age': 26, 'city': 'London'})
Query data:
python
data = client.get('table:example:id1')
print(data) # {'name': 'John', 'age': 30, 'city': 'New York'}
Delete data:
python
client.delete('table:example:id3')
In the above example, the key 'table: example:<id>' is used to store each piece of data, making it easy to find and delete. You can use different key naming methods according to specific needs.