Home Database Mysql Tutorial 基于redis内存数据库简单使用

基于redis内存数据库简单使用

Jun 07, 2016 pm 03:56 PM
redis use Memory based on database Simple

在ecplise中使用内存数据的客端户,前提要准备要下载两个jar包 commons-pool2-2.0.jar jedis-2.4.2.jar 前提准备做好了,那我们就开启redis的服务,打开一个命令窗口输入如下命令:redis-server 或redis-server redis根目\redis.conf 服务器已经开启了,注意

在ecplise中使用内存数据的客端户,前提要准备要下载两个jar包

commons-pool2-2.0.jar

jedis-2.4.2.jar

前提准备做好了,那我们就开启redis的服务,打开一个命令窗口输入如下命令:redis-server 或redis-server redis根目\redis.conf

\

服务器已经开启了,注意端号是6377 

2.在eclipse 创建一个项目,把redist需要的包导入项目中

\

3.写一个Jedis工具类

public class JedisUtil {

private static String HOST="127.0.0.1"; //本机地址

private static Integer PORT=6379; //服务端口

private static JedisPoolConfig config; //连接池的配置对象

private static JedisPool pool; //连接池对象

static{

config = new JedisPoolConfig();

config.setMaxIdle(1024*10); //设置内存大小

pool = new JedisPool(config,HOST);

}

/**

*

* @return 从连接池里获取一个Jedis对象

*/

public static Jedis getPoolJedis(){

return pool.getResource();

}

/**

* 手动把Jedis对象,回放到连接池里

*/

public static void returnPoolJedis(Jedis jedis){

pool.returnResource(jedis);

}

/**

* @return 直接创建一个Jedis连接

*/

public static Jedis getJedis(){

return new Jedis(HOST, PORT);

}

}

4.写一个客户端类操作jedis

public class Client {

public static void main(String[] args) {

//simpleSet();

mSet();

}

private static void mSet() {

//注意 如果使用 JedisUtil.getJedis(); 是直接创建一个jredis对象,所以不受连接池管理,所以不能回放到连接池里

Jedis jedis = JedisUtil.getPoolJedis();

//设置值多个值

jedis.mset("userName","user1","pwd","123");

//取值

List list = jedis.mget("userName","pwd");

for (String string : list) {

System.out.println(string);

}

jedis.flushDB();

JedisUtil.returnPoolJedis(jedis);

}

private static void simpleSet() {

Jedis jedis = JedisUtil.getPoolJedis();

//设置值

jedis.set("userName", "user1");

//取值

System.out.println("userName:"+jedis.get("userName"));

//清空内存数据库

jedis.flushDB();

JedisUtil.returnPoolJedis(jedis);

}

}

现在可以在redist客户端对数据进行取值

打开一个命令窗口,输入命令 redis-cli 开启客户端,原后进行赋值

\

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1244
24
How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to configure Lua script execution time in centos redis How to configure Lua script execution time in centos redis Apr 14, 2025 pm 02:12 PM

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to implement redis counter How to implement redis counter Apr 10, 2025 pm 10:21 PM

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles