Integrating Redis with Java for a Seckill Application and Optimizing with MySQL Stored Procedures
This guide walks through downloading and installing Redis, adding Java dependencies, creating a Redis DAO with Protostuff serialization, configuring Spring beans, caching seckill data, implementing a MySQL stored procedure for atomic execution, mapping it with MyBatis, updating service and controller layers, and testing the entire workflow.
Download and extract the Redis package, then compile and install it using make and sudo make install, finally start the server with redis-server and verify with redis-cli -p 6379.
Add Maven dependencies for jedis, protostuff-core, protostuff-runtime, and commons-collections to the project’s pom.xml.
Create a RedisDao class that manages a JedisPool, uses Protostuff RuntimeSchema for serializing and deserializing Seckill objects, and provides getSeckill and putSeckill methods.
Configure the DAO as a Spring bean in applicationContext-dao.xml with constructor arguments for host and port, and inject it into the service layer.
Modify the exportSeckillUrl method to first attempt to retrieve a Seckill object from Redis; if missing, load it from the database, cache it in Redis, and then continue with the normal flow.
Write a MySQL stored procedure execute_seckill that performs the insert‑ignore, row‑count checks, inventory decrement, and transaction control, returning a result code.
Map the procedure in MyBatis with a select statement (statementType="CALLABLE") and add a killByProcedure method to SeckillMapper that accepts a parameter map.
Implement executeSeckillProcedure in SeckillServiceImpl to build the parameter map, invoke the mapper, read the result from the map, and return the appropriate SeckillExecution based on the outcome.
Update SeckillController to call executeSeckillProcedure instead of the regular method, handling the same exception types, and add a JUnit test method executeSeckillProcedureTest to verify the end‑to‑end process.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
