How to clear cache in SpringBoot java

 Clear or evict all cache Programmatically.

Does Java provide something which can clear all the cache at once?👆👆👆👆❓❓❓

The answer is NO 👎👎👎😕😕😕.

Java does not provide any such feature which can clear all the cache at once.👍👍👍👍

So how do clear all the cache?😞😞😞

We have a solution which we can use to achieve this,😀😄✌👌

CacheManager class has a method name getCacheNames() returns the name of the cache, and after that we use clear() to clear that cache.

we use this method to achieve our goal to clear all the cache.

CacheManager.getCacheNames().

CacheManager.getCache(cacheName).clear

Find the code below 👇👇👇👍

@Controller
@CrossOrigin("*")
@RequestMapping("javaoneworld")
public class JavaOneWorldController {

	/* autowire cache manager */
	@Autowired
    private CacheManager cacheManager;      
    
	/* clear all cache using cache manager */
    @RequestMapping(path = "/clearCache")
    public ResponseEntity<String> clearCache(){
    	
    	if(cacheManager.getCacheNames().isEmpty()) {
    		return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No cache record has been found");
    	}else {

        	
            for(String name:cacheManager.getCacheNames()){
            	/* printing cache name */
                System.out.println(name);
				
    			/* clear cache by name */
                cacheManager.getCache(name).clear();
				
            }
    		return ResponseEntity.status(HttpStatus.OK).body("All cache has been cleared");
    	}
    }
	

}

 

Find another must-read post.


 

Java program to Count number of digit in an Integer

 Count digit in a given number  👐👐👐!!!!!!!!!!!!!!!!!


Example - 

if given number -->

X = 7654;

result ->  4


X = 765;

result ->  3


X = 76;

result ->  2



public class CountDigit {
    public static void main(String[] args) {
        //count digits in a given number

        int x = 9876;
        int result =0;
        while(x>0)
        {
            x = x/10;
            result ++;
        }

        System.out.println("Total digit -> "+result);
        //in this case result will be 4 
    }
}
     
Time complexity: θ(d)
where d is the number of the digit in the input number.

Find another must-read post.




Find odd even using bitwise operator

 As we have already seen in the previous post to swap two numbers without using the third variable and using the bitwise operator.👇

https://www.javaoneworld.com/2020/03/swap-two-numbers-using-bitwise-operator.html

 As I already mentioned that bitwise operators are always computational fast than any mathematical operators.



Why bitwise operators are computational fast than Mathematical operators 👈

so using a bitwise operator instead of a mathematical operator is always a good approach.

Here we are using & operator to find that number is Odd or Even.

public class JavaOneWorld {
    public static void main(String args[]) {
    int x= 7;

if((x&1)==0){
System.out.println("even");
}else{
System.out.println("odd");
}
    }
}



The idea behind using the & operator !!!!!

As we already know binary works only on 1 and 0.
and a number is either odd or even,
so if a number is even then the last bit of that number must be 0 and if odd then the last bit must be 1.

i.e.-

5     odd
101 last bit is 1.

4 even

100 last bit is 0.

so here we have to use this property to find out the odd/even.

if we & 1 in any binary number it will result ---

1    --- if the given number's last bit will 1 (means number is odd)

0    --- if the given number's last bit will 0 (means number is even)
   

7  -- odd

       111
&       1
--------------                            
      001

4  -- even

       100
&       1
--------------                                  
      000

   
***********************
***********************
***********************

Happy Coding !!!!!!!!!!!👍👍👍👍👍

follow us on Instagram @javaoneworld

another must-read blog !!!!!!!!!!!!!!👇👇👇👇👇







Android Interview Question For Senior Android Developer