In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1. About Redis
Redis is an open source software project (BSD license) written in ANSI C for most POSIX systems and is an in-memory database that can be used as a database, cache, and message broker. Redis is a non-relational database that stores mappings between keys and five different types of data structures: strings, lists, sets, ordered sets, and hashes. Redis usually keeps the entire dataset in memory, and Redis achieves persistence in two different ways: one is a snapshot, and the other is an Append Only File (AOF). Redis supports master-slave replication, and data from any Redis server can be replicated to any number of slaves. Redis also includes publish/subscribe, Lua scripting, and other features.
2. resource acquisition
Download third-party middleware resources on the official website. The official download website of Redis is https://redis.io/download. The version used in this article is redis-3.2.8. After downloading, extract the file. The file directory is shown in Figure 2-1.
Figure 2.1 Redis unzipped file
The src directory contains Redis implementations, the tests directory contains unit tests implemented in tcl, the deps directory contains libraries required by Redis, and SylixOS only needs to provide the POSIX compatible interface of libc and a C compiler.
3. Linux platform compilation
Copy the folder to Linux platform. Because there is Makefile file in the folder, it can be compiled directly. Makefile file is shown in Figure 3-1.
Figure 3.1 Makefile
Redis can manually select the memory allocator for compilation. Redis supports libc malloc and jemalloc, and jemalloc is the default value on Linux systems. Therefore, to force libc malloc compilation, you need to add command parameters. The compilation command is shown in Figure 3-2.
Figure 3.2 Compile Command
4. transplant work
Redis adopts the server-client model and supports command line mode and hiredis API interface, so it is necessary to compile the executable program of the server and the executable program of the client according to specific requirements.
4.1 Server Engineering Creation
Create a redis-server application project in Real-Evo IDE, delete the redis-server.c file under src directory in the project, and import src and deps folders in the source package. The imported project file is shown in Figure 4-1.
Figure 4-1 Engineering Documents
Change to expert mode, modify source file and header file path, header file path is shown in Figure 4-2.
Figure 4.2 Header file path
Part of the source file is shown in Figure 4 3.
Figure 4.3 Source file names
Link the cextern dynamic library in the base project, as shown in Figure 4-4.
Figure 4-4. Link cextern dynamic library
Because SylixOS supports the endian.h header file, comment some of the code in config.h and include the header file directly, as shown in Listing 4-1.
Listing 4-1 config.h code modification
#if 0
#ifndef BYTE_ORDER
#if (BSD >= 199103)
# include
#else
#if defined(linux) ||defined(__linux__)
# include
#else
#define LITTLE_ENDIAN 1234 /* least-significant byte first (vax,pc) */
#define BIG_ENDIAN 4321 /* most-significant byte first(IBM, net) */
#define PDP_ENDIAN 3412 /* LSB first in word, MSW first inlong (pdp)*/
#if defined(__i386__) ||defined(__x86_64__) || defined(__amd64__) || \
defined(vax) || defined(ns32000) || defined(sun386) || \
defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
defined(__alpha__) || defined(__alpha)
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#if defined(sel) || defined(pyr) ||defined(mc68000) || defined(sparc) || \
defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370)|| \
defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX)||\
defined(apollo) || defined(__convex__) || defined(_CRAY) || \
defined(__hppa) || defined(__hp9000) || \
defined(__hp9000s300) || defined(__hp9000s700) || \
defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
#define BYTE_ORDER BIG_ENDIAN
#endif
#endif/*linux */
#endif/* BSD */
#endif/* BYTE_ORDER */
#endif
#include
Part of the code variables are defined at the time of use, and the error is shown in Figure 4-5.
Figure 4-5. Errors defined in use.
Modify the compiler options as shown in Figure 4-6.
Figure 4-6. Configuring the compiler
There are multiple main entries in the code. Since you need to generate the executable file of the server, you need to comment out the redundant main entries. You only need to keep the main entry of server.c.
The function names in some codes are consistent, which leads to compilation errors. Therefore, it is necessary to add static modification to the corresponding function, and the error is shown in Figure 4-7.
Figure 4.7 Function redefinition error
Because the default stack space size of SylixOS cannot meet the stack space requirements of the server, the stack space size is modified, as shown in Listing 4-2.
Listing 4-2 Setting Stack Space Size
int main(intargc,char **argv) {
LW_CLASS_THREADATTR threadAttr;
LW_HANDLE hThreadId;
__GiArgc = argc;
__GppcArgv = argv;
Lw_ThreadAttr_Build(&threadAttr,
350 * LW_CFG_KB_SIZE,
LW_PRIO_NORMAL,
LW_OPTION_THREAD_STK_CHK,
LW_NULL);
hThreadId = Lw_Thread_Create("t_server", redisServer, &threadAttr, LW_NULL);
if (hThreadId ==LW_OBJECT_HANDLE_INVALID) {
return (PX_ERROR);
}
Lw_Thread_Join(hThreadId,NULL);
return ERROR_NONE;
}
The implementation of rename under SylixOS is to replace the original file directly with the new file. Since the original file already exists, it may cause the replacement to fail. Therefore, the original file needs to be deleted before renaming. The specific code is shown in Listing 4-3.
Listing 4 3 Use of rename
unlink(filename);
if (rename(tmpfile,filename) == -1) {
serverLog(LL_WARNING,"Error movingtempappend only file on the final destination: %s",strerror(errno));
unlink(tmpfile);
return C_ERR;
}
The fork in the project file is functionally replaced by the posix_spawn function. After the replacement is completed, compile the project and generate the executable program of the server.
4.2 Dynamic Library Engineering Creation
Create libredis dynamic library project, delete libredis.c file, import hiredis folder, project as shown in Figure 4-8.
Figure 4-8 Dynamic Library Engineering
Link to the cextern dynamic library and modify the source and header directories in expert mode, as shown in Figure 4-9.
Figure 4.9 Source and header file paths
The compilation is complete.
5. Use of Redis
Run the server. redis.conf is the server configuration file, which can configure the server functions. Run the server as shown in Figure 5-1.
Figure 5.1 Running Redis Server
Run the test program to test whether the API function of Redis is normal. The running result shows that the function is normal, as shown in Figure 5-2.
Figure 5-2 Test Case Run Results
6. resources
Official website:
https://redis.io/
Blog:
http://blog.csdn.net/kingqizhou/article/details/8104693
Documents:
Redis Command Reference Manual
Redis Combat
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.