{"id":18760830,"url":"https://github.com/sarakhild/spring-boot-redis-cache","last_synced_at":"2026-05-10T03:47:54.092Z","repository":{"id":226466826,"uuid":"768735380","full_name":"SaraKhild/spring-boot-redis-cache","owner":"SaraKhild","description":"This project simplify the Redis with Spring Boot","archived":false,"fork":false,"pushed_at":"2024-07-06T13:58:27.000Z","size":80,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-29T04:51:31.102Z","etag":null,"topics":["redis","spring-aop","spring-boot"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SaraKhild.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-07T16:29:14.000Z","updated_at":"2024-07-06T13:58:30.000Z","dependencies_parsed_at":"2024-07-06T15:08:10.726Z","dependency_job_id":null,"html_url":"https://github.com/SaraKhild/spring-boot-redis-cache","commit_stats":null,"previous_names":["sarakhild/spring-boot-redis-cache"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaraKhild%2Fspring-boot-redis-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaraKhild%2Fspring-boot-redis-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaraKhild%2Fspring-boot-redis-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaraKhild%2Fspring-boot-redis-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SaraKhild","download_url":"https://codeload.github.com/SaraKhild/spring-boot-redis-cache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239653111,"owners_count":19675034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["redis","spring-aop","spring-boot"],"created_at":"2024-11-07T18:14:00.363Z","updated_at":"2025-12-03T21:30:13.422Z","avatar_url":"https://github.com/SaraKhild.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Project by Using SpringBoot and Redis Caching Concepts\r\n\u003cbr\u003e\r\n\r\n## Overview\r\nThe main objective of this project is to optimize performance by integrating Redis Caching into this project.This is achieved through the creation of a custom annotation called @LogExecutionTime, which is designed to work seamlessly with Spring AOP. By annotating methods with @LogExecutionTime, we can accurately measure the execution time of these methods both before and after utilizing Redis Cache to fetch data from the database. This provides valuable insights into the efficiency gains achieved through caching, allowing us to assess the impact of caching on method execution times. \r\n \u003cbr\u003e\r\n \r\n## Usages\r\n-  SpringBoot\r\n-  SpringAop\r\n-  Redis\r\n-  MySQL\r\n\r\n## Architecture of the Project\r\n\r\n ### 1- src folders\r\n   - Controller\r\n   - Model \r\n   - Aspect\r\n   - Service\r\n   - Repository\r\n \r\n ### 2-Maven pom.xml\r\n\u003cbr\u003e\r\n\r\n```\r\n \u003cdependencies\u003e\r\n\t\t\u003cdependency\u003e\r\n\t\t\t\u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\r\n\t\t\t\u003cartifactId\u003espring-boot-starter-data-jpa\u003c/artifactId\u003e\r\n\t\t\u003c/dependency\u003e\r\n\t\t\u003cdependency\u003e\r\n\t\t\t\u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\r\n\t\t\t\u003cartifactId\u003espring-boot-starter-data-redis\u003c/artifactId\u003e\r\n\t\t\u003c/dependency\u003e\r\n\t\t\u003cdependency\u003e\r\n\t\t\t\u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\r\n\t\t\t\u003cartifactId\u003espring-boot-starter-web\u003c/artifactId\u003e\r\n\t\t\u003c/dependency\u003e\r\n\t\t\u003cdependency\u003e\r\n\t\t\t\u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\r\n\t\t\t\u003cartifactId\u003espring-boot-starter-aop\u003c/artifactId\u003e\r\n\t\t\u003c/dependency\u003e\r\n\t\t\u003cdependency\u003e\r\n\t\t\t\u003cgroupId\u003ecom.mysql\u003c/groupId\u003e\r\n\t\t\t\u003cartifactId\u003emysql-connector-j\u003c/artifactId\u003e\r\n\t\t\t\u003cscope\u003eruntime\u003c/scope\u003e\r\n\t\t\u003c/dependency\u003e\r\n\t\t\u003cdependency\u003e\r\n\t\t\t\u003cgroupId\u003eorg.projectlombok\u003c/groupId\u003e\r\n\t\t\t\u003cartifactId\u003elombok\u003c/artifactId\u003e\r\n\t\t\t\u003coptional\u003etrue\u003c/optional\u003e\r\n\t\t\u003c/dependency\u003e\r\n\t\t\u003cdependency\u003e\r\n\t\t\t\u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\r\n\t\t\t\u003cartifactId\u003espring-boot-starter-test\u003c/artifactId\u003e\r\n\t\t\t\u003cscope\u003etest\u003c/scope\u003e\r\n\t\t\u003c/dependency\u003e\r\n\u003c/dependencies\u003e\r\n ```\r\n\u003cbr\u003e\r\n\r\n### 3-Application.properties.yml\r\n\r\n```\r\n# Redis Config\r\nspring.cache.type=redis\r\nspring.redis.host=localhost\r\nspring.redis.port=6379\r\n\r\n# Database Config\r\nspring.jpa.hibernate.ddl-auto=update\r\nspring.datasource.url=jdbc:mysql://localhost:3312/db\r\nspring.datasource.username=db\r\nspring.datasource.password=nFLhPPKOnkW1FA1e\r\nspring.jpa.show-sql=true\r\n\r\n ```\r\n## Let's Start :mechanical_arm:\r\n \r\n##### :pencil2: Create entity called product. Since Redis is an in-memeory database, we need to transform our object into stream of bytes for storing  as well as the other way around for retrieving data. So we nedd to serialize/deserialize by implements the Serializable class.\r\n\r\n###### Code :computer:\r\n```\r\n@Entity \r\n@Data \r\n@AllArgsConstructor \r\n@NoArgsConstructor \r\npublic class Product implements Serializable {\r\n\r\n    @Id\r\n    @GeneratedValue(strategy = GenerationType.IDENTITY)\r\n    private long id;\r\n    private String name;\r\n    private String code;\r\n    private int quantity;\r\n    private double price; \r\n}\r\n```\r\n##### :pencil2: We have repository for basic CRUD operations. \r\n###### Code :computer:\r\n```\r\n@Repository\r\npublic interface ProductRepository extends JpaRepository\u003cProduct, Long\u003e {\r\n}\r\n```\r\n##### :pencil2: @EnableCaching annotation that's will trigger inspects every Spring bean for the presence of caching annotations on methods.\r\n###### Code :computer:\r\n```\r\n@SpringBootApplication\r\n@EnableCaching\r\npublic class SpringDataWithRedisApplication {\r\n\r\n  public static void main(String[] args) {\r\n    SpringApplication.run(SpringDataWithRedisApplication.class, args);\r\n\t}\r\n   }\r\n```\r\n##### :pencil2: @Cacheable is used to bring data from database and storing it in the cache. In future, the method retrives data from the cache value directly.The value attribute establishes a cache with a specific name and the key is a unique identifier that represents a specific piece of cached data. Accordingly, the method result is stored in the \"product\" cache, where respective \"id\" of product as the unique key.\r\n###### Code :computer: \r\n```\r\n@Cacheable(key = \"#id\", value = \"product\")\r\npublic Product getById(long id) {\r\n   return repository.findById(id).get();\r\n  }\r\n```\r\n##### :pencil2: @Caching is used for multiple nested caching on the same method. Inside @CacheEvict the allEntries attribute allow us to remove all given cache by make it as true with another cache name then we going to update data in the cache using @CachePut.\r\n###### Code :computer:\r\n```\r\n@Caching(evict = { @CacheEvict(value = \"evictProduct\", allEntries = true) }, put = {\r\n         @CachePut(key = \"#id\", value = \"product\") })\r\npublic Product update(Product model, long id) {\r\n   var product = repository.findById(id).get();\r\n\r\n   product.setName(model.getName());\r\n   product.setCode(model.getCode());\r\n   product.setPrice(model.getPrice());\r\n   product.setQuantity(model.getQuantity());\r\n   repository.save(model);\r\n\r\n   return product;\r\n    }\r\n```\r\n##### :pencil2: @CacheEvict is used for removing data from the cache based on id product identifier.\r\n###### Code :computer:\r\n```\r\n@CacheEvict(key = \"#id\", cacheNames = \"product\")\r\npublic void deleteById(long id) {\r\n repository.deleteById(id);\r\n    }\r\n```\r\n##### :pencil2: As you can see the time taken when I invovic the specific product id from database as 1800ms. While, when I invoviced it again but this time it going fetch it from cache as 16ms. If you notic the last line when I invoviced  it once another time  the excutation time will be less as 8ms.\r\n###### Result :star_struck:\r\n\r\n\u003cbr\u003e\r\n\r\n\u003cimg width=\"587\" alt=\"output\" src=\"https://github.com/SaraKhild/spring-boot-redis-cache/assets/67427643/82a67f28-1315-449a-8b21-6059d1fdf030\"\u003e\r\n  \r\n---\r\n\u003cbr\u003e\r\n\r\n### Good Luck \u003cimg src=\"https://media.giphy.com/media/hvRJCLFzcasrR4ia7z/giphy.gif\" width=\"30px\"\u003e \r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsarakhild%2Fspring-boot-redis-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsarakhild%2Fspring-boot-redis-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsarakhild%2Fspring-boot-redis-cache/lists"}